如何使用 curl 放置一个带有数组的 json 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15425446/
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 18:48:50  来源:igfitidea点击:

How to PUT a json object with an array using curl

jsoncurl

提问by Miles Libbey

I have a series of data to enter into database. The user interface to enter the data isn't good for bulk entry, so I'm trying to formulate a command line equivalent. When I examine the network request of the UI in chrome, I see a PUT request of a json object. When I try to replicate the request

我有一系列数据要输入数据库。输入数据的用户界面不适合批量输入,所以我试图制定一个等效的命令行。当我在 chrome 中检查 UI 的网络请求时,我看到一个 json 对象的 PUT 请求。当我尝试复制请求时

curl -H 'Accept: application/json' -X PUT '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`

I get a error

我收到一个错误

curl: (3) [globbing] nested braces not supported at pos X

curl: (3) [globbing] 位置 X 不支持嵌套大括号

Where X is the character position of first "[".

其中 X 是第一个“[”的字符位置。

How can I PUT a json object that includes an array?

如何放置包含数组的 json 对象?

回答by Daniel Stenberg

Your command line should have a -d/--datainserted before the string you want to send in the PUT, and you want to set the Content-Type and not Accept.

您的命令行应该在要在 PUT 中发送的字符串之前插入-d/--data,并且您要设置 Content-Type 而不是 Accept。

curl -H 'Content-Type: application/json' -X PUT -d '[JSON]' http://example.com/service

Using the exact JSON data from the question, the full command line would become:

使用问题中的确切 JSON 数据,完整的命令行将变为:

curl -H 'Content-Type: application/json' -X PUT \
-d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' \
http://example.com/service

回答by Yonik

Although the original post had other issues (i.e. the missing "-d"), the error message is more generic.

尽管原始帖子有其他问题(即缺少“-d”),但错误消息更为通用。

curl: (3) [globbing] nested braces not supported at pos X

curl: (3) [globbing] 位置 X 不支持嵌套大括号

This is because curly braces {} and square brackets [] are special globbing characters in curl. To turn this globbing off, use the "-g" option.

这是因为大括号 {} 和方括号 [] 是 curl 中的特殊通配符。要关闭此通配符,请使用“ -g”选项。

As an example, the following Solr facet query will fail without the "-g" to turn off curl globbing: curl -g 'http://localhost:8983/solr/query?json.facet={x:{terms:"myfield"}}'

例如,以下 Solr facet 查询将在没有“-g”关闭 curl globbing 的情况下失败: curl -g 'http://localhost:8983/solr/query?json.facet={x:{terms:"myfield"}}'

回答by mogul

It should be mentioned that the Acceptheader tells the server something about what we are accepting back, whereas the relevant header in this context is Content-Type

应该提到的是,Accept标头告诉服务器一些我们正在接受的信息,而这个上下文中的相关标头是Content-Type

It's often advisable to specify Content-Typeas application/jsonwhen sending JSON. For curl the syntax is:

通常建议在发送 JSON 时指定Content-Typeapplication/json。curl 的语法是:

-H 'Content-Type: application/json'

So the complete curl command will be:

所以完整的 curl 命令将是:

curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X PUT -d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1"},{"id":"a1","answer":"answer2"}]}' http://example.com/service`

回答by vibs2006

Try using a single quoteinstead of double quotes along with -g

尝试使用单引号而不是双引号和-g

Following scenario worked for me

以下场景对我有用

curl -g -d '{"collection":[{"NumberOfParcels":1,"Weight":1,"Length":1,"Width":1,"Height":1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user [email protected]:123456 -X POST  https://yoururl.com

WITH

curl -g -d "{'collection':[{'NumberOfParcels':1,'Weight':1,'Length':1,'Width':1,'Height':1}]}" -H "Accept: application/json" -H "Content-Type: application/json" --user [email protected]:123456 -X POST  https://yoururl.com

This especially resolvedmy error curl command error : bad url colon is first character

这特别解决了我的错误 curl 命令错误:错误的 url 冒号是第一个字符

回答by Chen

The only thing that helped is to use a file of JSON instead of json body text. Based on How to send file contents as body entity using cURL

唯一有帮助的是使用 JSON 文件而不是 json 正文文本。基于如何使用 cURL 将文件内容作为正文实体发送