69

How can I make this work?

curl -XPOST 'http://localhost:9290/location/place' -d '{"geoloc": {"lat": "38.1899", "lon": "-76.5087"}, "longitude": "-76.5087", "admin_name1": "Maryland", "admin_name2": "St. Mary's", "admin_name3": "", "postal_code": "20692", "admin_code3": "", "country_code": "US", "admin_code1": "MD", "latitude": "38.1899", "admin_code2": "037", "accuracy": null, "place_name": "Valley Lee"}'

The ' in Mary's is causing this to fail. I am running it from a file like cat curl-cmd.txt | sh but it won't work from the command line either. I've tried using \' and \\' and \u0027 (the unicode ')

I'm stuck

Travis Clarke
  • 5,951
  • 6
  • 29
  • 36
mikeb
  • 10,578
  • 7
  • 62
  • 120

2 Answers2

154

I had the same problem. The simplest solution is to escape the apostrophe with a backslash in addition to wrapping it in a set of single quotes. '\''

For your use case, change Mary's to Mary'\''s and it should work.

curl -XPOST 'http://localhost:9290/location/place' -d '{"geoloc": {"lat": "38.1899", "lon": "-76.5087"}, "longitude": "-76.5087", "admin_name1": "Maryland", "admin_name2": "St. Mary'\''s", "admin_name3": "", "postal_code": "20692", "admin_code3": "", "country_code": "US", "admin_code1": "MD", "latitude": "38.1899", "admin_code2": "037", "accuracy": null, "place_name": "Valley Lee"}'

An alternate approach is to wrap the POST data (-d) in double quotes while escaping all nested occurrences of double quotes in the JSON string with a backslash.

curl -XPOST 'http://localhost:9290/location/place' -d "{\"geoloc\": {\"lat\": \"38.1899\", \"lon\": \"-76.5087\"}, \"longitude\": \"-76.5087\", \"admin_name1\": \"Maryland\", \"admin_name2\": \"St. Mary's\", \"admin_name3\": \"\", \"postal_code\": \"20692\", \"admin_code3\": \"\", \"country_code\": \"US\", \"admin_code1\": \"MD\", \"latitude\": \"38.1899\", \"admin_code2\": \"037\", \"accuracy\": null, \"place_name\": \"Valley Lee\"}"
Travis Clarke
  • 5,951
  • 6
  • 29
  • 36
  • 1
    For some reason I needed your crazy solution for calling curl from PHP's shell_exec(), even though I'm using `--data-urlencode` Maybe has something to do with the way Linux processes the string before it gets to curl? Not sure. – PJ Brunet Jan 21 '18 at 09:23
  • First solution does not work on my Mac's console it produces the same effect as not escaping at all. – Slobodan Antonijević Feb 01 '18 at 13:56
  • 1
    @PJBrunet - I presume it is the shell performing string concatenation on the command line. (e.g. `echo 'hello'\''world'` -> `hello'world`) – Travis Clarke Feb 03 '18 at 21:58
  • Can we Implement this in [URI Search](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html)? – ASH Oct 21 '18 at 17:09
  • @ASH – I am not sure if I understand the question; do you have a specific request in mind? This technique (i.e. shell string concatenation) should work for any `curl` request or command for that matter, not just this specific Elasticsearch request, as long as you are using a modern shell (e.g. bash, zsh, sh). – Travis Clarke Oct 25 '18 at 05:25
  • how can we parse single quote when is in a variable and that variable is the input for post – Sunil Agarwal Jan 24 '19 at 14:19
  • Great crazy solution. If you need this as a Java replacement, it gets even uglier: `s.replace("'", "'\\''");` – Alphaaa Apr 09 '19 at 12:02
  • For me it didn't work if the first parameter (e.g. -XPOST) has single quote, but the second(e.g. -d) has double quote. I had to change to double quote all parameters consistently – Michael Freidgeim Dec 07 '20 at 04:51
15

Rule Of Thumb: In case you want explicitly representing single quote or double quotes in your string on bash, Use backslash (\) depends on your String Wrapper (should be in the same type). The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

Examples:

-Double Quote Example - Use \"

in case you want to print on bash She said "Yes I Do"

echo "She said \"Yes I Do\""
#output:
She said "Yes I Do"

echo 'she said "Yes I Do"' 
#output:
She said "Yes I Do"

-Single Quote example - Use '\''

in case you want to print on bash My Daughter's dog likes cat treats

echo "My Daughter's dog likes cat treats"
#output:
My Daughter's dog likes cat treats

echo 'My Daughter'\''s dog likes cat treats' 
#output:
My Daughter's dog likes cat treats
Community
  • 1
  • 1
avivamg
  • 12,197
  • 3
  • 67
  • 61