欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to PUT a json object with an array using curl 有大用

115

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`

I get a error

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

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

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

5 Answers     正确答案

172

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

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

Note: JSON data wrapped only for readability, not valid for curl request.

  • 4
    -1 because Content-Type (instead of Accept) is the header that should be set in this case. 
    – Goran
     Jul 11, 2013 at 13:23
  • 5
    For those who are wondering, [JSON] is simply a placeholder for the JSON string. Don't add extra square brackets around your JSON string.  Aug 6, 2015 at 17:04
107

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

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

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

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"}}'

40

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

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

-H 'Content-Type: application/json'

So the complete curl command will be:

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`
3

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

2

Try using a single quote instead of double quotes along with -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 test@testmail.com: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 test@testmail.com:123456 -X POST  https://yoururl.com

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

Your Answer

来自  https://stackoverflow.com/questions/15425446/how-to-put-a-json-object-with-an-array-using-curl


普通分类: