35

Here is an example of command line that fit this description :

curl  http://dumbdomain.com/solr/collection2/update/json -H
'Content-type:application/json' -d ' { "add": { "doc": { "uid":
"79729", "text" : "I''ve got your number"} } }'

I already tried \' (not escaped), url encoded (not urldecoded at this other end!) and '' (quote disappear!), without success.

tkruse
  • 10,222
  • 7
  • 53
  • 80
Pr Shadoko
  • 1,649
  • 2
  • 15
  • 16

4 Answers4

62

If you replace ' by unicode encoded ' (which is \u0027), then it works:

curl http://dumbdomain.com/solr/collection2/update/json -H 'Content-type:application/json' -d ' { "add": { "doc": { "uid": "79729", "text" : "I\u0027ve got your number"} } }'

Strange, but worth to know!

Pr Shadoko
  • 1,649
  • 2
  • 15
  • 16
21

An usual workaround in such cases is to put the data in a file and post.

$ cat post.json
{ "add": { "doc": { "uid": "79729", "text" : "I've got your number"} } }

And then invoke:

curl -H "Content-type:application/json" --data @post.json http://dumbdomain.com/solr/collection2/update/json

This would obviate the need of escaping any quotes in the json.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • Yeah, seen that in related posts, but I don't want to make it slower and more complex just to escape one char! There must be a way. BTW, I found something and I'm testing it. – Pr Shadoko Sep 04 '13 at 11:32
  • awesome fix for instances where you need to do a lot of escaping. i'm also looking into a quick little find and replace call to replace ' and " with their \u0022 and \u0027 counterparts – Chad Stovern Oct 16 '14 at 06:35
14

In case you're using Windows (this problem typically doesn't occur on *nix), you can pipe the output from echo to curl to avoid the escaping altogether:

echo {"foo": "bar", "xyzzy": "fubar"} | curl -X POST -H "Content-Type: application/json" -d @- localhost:4444/api/foo
fo_
  • 872
  • 8
  • 8
3

Do you mean how to get the JSON passed via the command line correctly? If you're using Windows then you need to be careful how you escape your string. It works if you use double quotes around the whole data string and then escape the double quotes for the JSON. For example:

curl http://dumbdomain.com/solr/collection2/update/json -H 'Content-type:application/json' -d "{ \"add\": { \"doc\": { \"uid\": \"79729\", \"text\" : \"I've got your number\"} } }"
dwxw
  • 1,089
  • 1
  • 10
  • 17
  • I've read round here that escaping with ^ on Windows should do the trick, but unfortunatly I'm batching this on CentOS. – Pr Shadoko Sep 04 '13 at 11:39