Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL escape single quote

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

like image 496
mikeb Avatar asked Aug 20 '15 15:08

mikeb


People also ask

How do you escape single quotes in curl?

The backslash ( \ ) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

What is escape single quote?

Quoting and Escaping. Single quotes tell Integration Engine to take what is enclosed in the single quotes literally. No escaping is used with single quotes. Use a double backslash as the escape character for backslash.

How do you escape double quotes in curl command?

You can most certainly escape double quotes. How you do that depends on your operating system and shell, which you fail to specify. On Windows, you'd use the ^ as the escape character.

How do you escape single and double quotes?

Backslash is used to start an escape sequence inside character constants. Escaping a character not in the following table is an error. Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings.


1 Answers

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\"}" 
like image 137
Travis Clarke Avatar answered Sep 28 '22 02:09

Travis Clarke