Gdfelt
Gdfelt

Reputation: 189

add body to a cloud scheduler request using the Python API

I'm extending off of this question: How to create a job with Google Cloud scheduler Python api

I'm wondering how I can insert a body object to be passed along with the function, I can do it via gcloud, and I know according to the v1 docs that the body needs to be passed in a HttpTarget any time I try to pass it that way it errors and says:

TypeError: No positional arguments allowed

Honestly I haven't been able to get from google.cloud.scheduler_v1.types import HttpTarget as Target to work at all.

Can someone give me an example where they successfully used the API to create a job in the Cloud Scheduler with a body (JSON object) sent with it (POST method of course)?

Upvotes: 3

Views: 1564

Answers (1)

Dustin Ingram
Dustin Ingram

Reputation: 21580

import json

from google.cloud import scheduler_v1

client = scheduler_v1.CloudSchedulerClient()

project = "..."  # TODO
location = "..."  # TODO
parent = client.location_path(project, location)

uri = "..."  # TODO
body = {"Hello": "World"}

job = {
    "http_target": {
        "http_method": "POST",
        "uri": uri,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps(body).encode("utf-8"),
    },
    "schedule": "* * * * *",
}

response = client.create_job(parent, job)

print(response)

Upvotes: 6

Related Questions