Bash – Proper escaping of double quotations for curl post data

bashcommand linecurlescape-charactersshell

I'm trying to test out a server, by sending it JSON data and reading the response.

I need to POST data like {"item":"value with spaces"} but when I use curl I find that it sends the backslashes as well as the double quotes:

curl -d "{\"item\":\"value with spaces\"}" http://myserver.com/somerubyapp?get=stuff

The server actually receives "{\"item\":\"value with spaces\"}" including the double quotes on the ends of the string, the backslashes, and everything.

Am I using curl incorrectly, or is it an issue with my shell, bash?

Best Answer

It's a kind of necroposting but I've had the same problem recently (with a different backend) and found that the reason is in a wrong Content-Type. By default it's "text/plain" or "text/html", and in my case curl -H "Content-Type: application/json" -d ... solved the issue.

Related Question