105

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?

mogul
  • 4,006
  • 1
  • 15
  • 22
Miles Libbey
  • 1,454
  • 2
  • 10
  • 9

5 Answers5

154

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.

Daniel Stenberg
  • 44,219
  • 12
  • 115
  • 175
  • 4
    -1 because Content-Type (instead of Accept) is the header that should be set in this case. – Goran Jul 11 '13 at 13:23
  • 4
    For those who are wondering, [JSON] is simply a placeholder for the JSON string. Don't add extra square brackets around your JSON string. – Mihai Todor Aug 06 '15 at 17:04
97

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

Yonik
  • 2,181
  • 17
  • 14
39

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`
mogul
  • 4,006
  • 1
  • 15
  • 22
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

vibs2006
  • 4,813
  • 2
  • 34
  • 34
1

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

Community
  • 1
  • 1
Chen
  • 175
  • 1
  • 8