简体   繁体   中英

Create a cloud scheduler job in CLI with nested json request body

I am running into trouble trying to schedule a Workflow using Google Cloud Scheduler through the Google CLI.

In particular my workflow requires the following request body:

{
"arg1": "some_string",
"arg2": "some_other_string",
"arg3": [
    {
      "foo": "foo1",
      "bar": "bar1"
    },
    {
      "foo": "foo2",
      "bar": "bar2"
    }
  ]
}

In a different workflow with request body consisting only of arg1 and arg2 I was able to schedule a cloud function using the double-escaped json string format:

gcloud scheduler jobs create http <NAME> --schedule=<CRON> --uri=<URI> --message-body="{\"argument\": \"{\\\"arg1\\\":\\\"some_string\\\",\\\"arg2\\\":\\\"some_other_string\\\"}\"}" --time-zone="UTC"

With the above request body I am unclear how to do this, I tried setting the message-body as

"{\"argument\": \"{\\\"arg1\\\":\\\"some_string\\\",\\\"arg2\\\":\\\"some_other_string\\\",\\\"arg3\\\":\\\"[{\\\\\"foo\\\\\":\\\\\"foo1\\\\\",\\\\\"bar\\\\\":\\\\\"bar1\\\\\"}]\\\"}\"}"

But it didn't seem to like this and threw an "INVALID ARGUMENT" status. I've also tried a few other variations such as without quotes around the list brackets but haven't had any success.

Apologies for how ugly these strings are. Is anyone aware how to format them correctly, or better yet, a simplified way of entering the request body in the command?

Thanks in advance.


Edit: I have tried using the --message-body-from-file argument as mentioned in the comments by @john-hanley. I found it still required escape quotes to work on my simple case.

body.json

{"argument": "{\"arg1\":\"some_string\",\"arg2\":\"some_other_string\"}"}

When I tried the nested case however with no quotes around the list it did work! body.json

{"argument": "{\"arg1\":\"some_string\",\"arg2\":\"some_other_string\", \"arg3\": [{\"foo\": \"foo1\", \"bar\": \"bar1\"},{\"foo\":\"foo2\", \"bar\": \"bar2\"}]"}

Solved by incorporating comments by @JohnHanley and unquoting the repeated field

Command:

gcloud scheduler jobs create http <NAME> --schedule=<CRON> --uri=<URI> --message-body-from-file="body.json" --time-zone="UTC"

body.json

{"argument": "{\"arg1\":\"some_string\",\"arg2\":\"some_other_string\", \"arg3\": [{\"foo\": \"foo1\", \"bar\": \"bar1\"},{\"foo\":\"foo2\", \"bar\": \"bar2\"}]"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM