简体   繁体   English

在 CLI 中使用嵌套的 json 请求正文创建云调度程序作业

[英]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.我在尝试通过 Google CLI 使用 Google Cloud Scheduler 安排工作流时遇到了麻烦。

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:在请求正文仅包含arg1arg2的不同工作流程中,我能够使用双转义的 json 字符串格式安排云 function:

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.但它似乎不喜欢这样,并抛出了“INVALID ARGUMENT”状态。 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.编辑:我尝试使用 @john-hanley 的评论中提到的--message-body-from-file参数。 I found it still required escape quotes to work on my simple case.我发现它仍然需要转义引号来处理我的简单案例。

body.json身体.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身体.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通过合并@JohnHanley 的评论并取消引用重复字段来解决

Command:命令:

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

body.json身体.json

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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