如何使用 curl 放置一个带有数组的 json 对象 [英] How to PUT a json object with an array using curl
问题描述
我有一系列数据要输入数据库.输入数据的用户界面不适合批量输入,所以我试图制定一个等效的命令行.当我在 chrome 中检查 UI 的网络请求时,我看到一个 json 对象的 PUT 请求.当我尝试复制请求时
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
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`
我收到一个错误
curl: (3) [globbing] pos X 不支持嵌套大括号
curl: (3) [globbing] nested braces not supported at pos X
其中X是第一个["的字符位置.
Where X is the character position of first "[".
如何放置包含数组的 json 对象?
How can I PUT a json object that includes an array?
推荐答案
你的命令行应该有一个 -d/--data 插入到要在 PUT 中发送的字符串之前,并且要设置 Content-Type 而不是 Accept.
Your command line should have a -d/--data inserted before the string you want to send in the PUT, and you want to set the Content-Type and not Accept.
curl -H 'Content-Type: application/json' -X PUT -d '[JSON]'
http://example.com/service
使用问题中的确切 JSON 数据,完整的命令行将变为:
Using the exact JSON data from the question, the full command line would become:
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
注意:JSON 数据仅用于可读性包装,对于 curl
请求无效.
Note: JSON data wrapped only for readability, not valid for curl
request.
这篇关于如何使用 curl 放置一个带有数组的 json 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!