Pr Shadoko
Pr Shadoko

Reputation: 1698

How to escape single quotes into double quotes into single quotes

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.

Upvotes: 35

Views: 48926

Answers (4)

fo_
fo_

Reputation: 922

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

Upvotes: 14

Pr Shadoko
Pr Shadoko

Reputation: 1698

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!

Upvotes: 64

dwxw
dwxw

Reputation: 1099

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

Upvotes: 3

devnull
devnull

Reputation: 123458

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.

Upvotes: 21

Related Questions