我有一系列数据要输入数据库。输入数据的用户界面不适合批量输入,所以我正在尝试制定一个等效的命令行。当我在 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`
我收到一个错误
curl: (3) [globbing] nested braces not supported at pos X
其中 X 是第一个“[”的字符位置。
如何放置一个包含数组的 json 对象?
最佳答案
你的命令行应该有 -d/--data插入到要在 PUT 中发送的字符串之前,并且要设置 Content-Type 而不是 Accept。
curl -H 'Content-Type: application/json' -X PUT -d '[JSON]' \
http://example.com/service
使用问题中的确切 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
注意:JSON 数据仅为可读性而包装,对 curl
请求无效。
关于json - 如何使用 curl 将 json 对象与数组一起放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15425446/