4

When you are creating a new cron job in Cloud Scheduler:

enter image description here

What should I add in the body field, in order to pass a parameter value:

What is the specification of that field? Should I write JSON in there?

Let's say I want to pass this JSON object:

{
  "foo": "bar"
}

Is the "Content-Type": "application/json" automatically added?

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336

1 Answers1

6

You can provide the information you consider appropriate in the body field.

At least in the case of AppEngineAppTarget- probably the behavior will be the same for HttpTarget, as indicated in the documentation when describing the headers field, they indicate that if the job has an body, Cloud Scheduler sets the following headers:

Content-Type: By default, the Content-Type header is set to "application/octet-stream". The default can be overridden by explictly setting Content-Type to a particular media type when the job is created. For example, Content-Type can be set to "application/json". ...

AFAIK, it is not possible to provide the Content-Type or any other custom header from the Google Cloud Web console, but you can use the gcloud CLI if you need to. Please, see the relevant documentation. Pay special attention to the OPTIONAL FLAGS section, and within it, to the --headers, --message-body and --message-body-from-file flags. Your command should look something like:

gcloud scheduler jobs create http job-name \
  --schedule="0 */3 * * *" \
  --uri="http://your.url.com" \
  --http-method=POST \
  --headers="Content-Type: application/json" \
  --message-body="{\"field1\":\"value1\",\"field2\":\"value2\"}}"

Please, see also this related SO questions 1 2, they could be of help.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
  • Thank you so much! Didn't know it was possible to add headers through `gcloud`. And how about when you inspect a cron job you've created with `gcloud` and have set some `headers`. How do they appear on the Cloud Scheduler Console? I mean, how can one verify which jobs have which headers? I guess `gcloud` has some kind of `--list` flag that might log all these details. – cbdeveloper Jan 28 '21 at 13:16
  • You are welcome @cbdeveloper. I honestly does not know if the Google Cloud Web console provides that level of detail for the job - I will dig into it, but the `gcloud` CLI provides the [`describe`](https://cloud.google.com/sdk/gcloud/reference/scheduler/jobs/describe) and [`list`](https://cloud.google.com/sdk/gcloud/reference/scheduler/jobs/list) commands that can be use to get information about a certain job or list, and optionally filter, sort, etc, your jobs, respectively. – jccampanero Jan 28 '21 at 13:37
  • Thank you again. That works fine. I just changed the headers flag to this: `--headers Content-Type=application/json`. They should really add a `headers` config to the Console. – cbdeveloper Jan 28 '21 at 13:40
  • Yes, I think so @cbdeveloper. – jccampanero Jan 28 '21 at 13:56
  • The header has been added to the web interface – dada May 06 '21 at 14:33