如何使用curl使用数组输出json对象

How to PUT a json object with an array using curl

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

1
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对象?


您的命令行应该在要发送到Put中的字符串之前插入-d/--数据,并且您希望设置内容类型而不接受。

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
curl -H 'Content-Type: application/json' -X PUT \
-d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1
<div class="suo-content">[collapse title=""]<ul><li>-1,因为Content-Type(而不是Accept)是在这种情况下应该设置的头。</li><li>对于那些想知道的人来说,[json]只是json字符串的一个占位符。不要在JSON字符串周围添加额外的方括号。</li></ul>[/collapse]</div><hr>
<p>
Although the original post had other issues (i.e. the missing"-d"), the error message is more generic.
</p>

<blockquote>
  <p>
curl: (3) [globbing] nested braces not supported at pos X
</p>
</blockquote>

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

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

<div class="suo-content">[collapse title=""]<ul><li>这对我来说是正确的解决方案,使用<wyn>-g</wyn>按预期工作。谢谢@yonik</li><li>天哪,我一直在寻找解决我的GRAPHQL卷曲问题的方法,这对我有帮助。冬虫夏草酱。</li></ul>[/collapse]</div><p><center>[wp_ad_camp_1]</center></p><hr>
<p>
It should be mentioned that the <wyn>Accept</wyn> header tells the server something about what we are accepting back, whereas the relevant header in this context is <wyn>Content-Type</wyn>
</p>

<p>
It's often advisable to specify <wyn>Content-Type</wyn> as <wyn>application/json</wyn> when sending JSON. For curl the syntax is:
</p>

[cc]-H 'Content-Type: application/json'

所以完整的curl命令是:

1
2
3
4
5
6
7
8
9
10
11
curl -H 'Content-Type: application/json' -H 'Accept: application/json' -X PUT -d '{"tags":["tag1","tag2"],"question":"Which band?","answers":[{"id":"a0","answer":"Answer1
<hr>
<p>
Try using a single quote instead of double quotes along with -g  
</p>

<p>
Following scenario worked for me
</p>

[cc]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

1
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

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


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