How to escape single quotes into double quotes into single quotes

后端 未结 4 1581
误落风尘
误落风尘 2020-12-03 02:52

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

curl  http://dumbdomain.com/solr/collection2/update/json -H
\'Content-type:applic         


        
相关标签:
4条回答
  • 2020-12-03 03:12

    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!

    0 讨论(0)
  • 2020-12-03 03:12

    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\"} } }"
    
    0 讨论(0)
  • 2020-12-03 03:26

    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.

    0 讨论(0)
  • 2020-12-03 03:26

    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
    
    0 讨论(0)
提交回复
热议问题