如何使用 JSON 参数 cURL 发布?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25435798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:29:55  来源:igfitidea点击:

How to cURL post with JSON parameters?

jsonpostcurl

提问by David T.

I'm not sure if this is possible, but i am trying to curl a post, but with a json as the parameters, like such:

我不确定这是否可行,但我正在尝试卷曲帖子,但使用 json 作为参数,如下所示:

curl -X POST 'https://myserver/action?params={"field1":"something","whatever":10,"description":"body","id":"random","__oh__":{"session":"12345678jhgfdrtyui"}}'

however, i keep getting some error curl: (3) [globbing] nested braces not supported at posX

但是,我不断收到一些错误curl: (3) [globbing] nested braces not supported at posX

how do i do this?

我该怎么做呢?

回答by Yonik

The curl error is due to braces {} and square brackets [] being special curl globbing characters. Use the -g option to turn off globbing and you should be fine.

卷曲错误是由于大括号 {} 和方括号 [] 是特殊的卷曲通配符。使用 -g 选项关闭通配符,你应该没问题。

Same issue as this post: How to PUT a json object with an array using curl

与这篇文章相同的问题: How to PUT a json object with an array using curl

回答by Martin Konecny

There two ways to approach this.

有两种方法可以解决这个问题。

  1. Ensure that your JSON is properly escaped so that it can be sent as a parameter.
  2. Set the HTTP header to accept json.
  1. 确保您的 JSON 正确转义,以便它可以作为参数发送。
  2. 将 HTTP 标头设置为接受 json。

For example:

例如:

curl -X POST -H "Content-Type: application/json" \
--data '{"field1":"something","whatever":10,"description":"body","id":"random","__oh__":{"session":"12345678jhgfdrtyui"}}' \
https://example.com/action