2

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?

  • 2
    Have you tried curl -d '{"item":"value with spaces"}'? – terdon May 20 '14 at 19:48
  • 1
    terdon's advice should work, because single quotes are supposed to negate any special characters between them: howtogeek.com/howto/29980/… – Darth Hunterix May 20 '14 at 19:58
  • Strangely enough, it adds quotes and backslashes if I do that. curl -d '{"item":"value with spaces"}' is received by the server as "{\"item\":\"value with spaces\"}" – Adam Davis May 20 '14 at 20:01
  • Just a wild guess, but I seem to remember that in curl the nested quoting works indeed the same way as in alias in bash. For instance, this is one of my many alias lines: alias hdd='lsblk -nio KNAME,MODEL,SIZE,MOUNTPOINT | grep -vi "\(1K\|sd[a-z][1-3]\s\+[0-9]\+,[0-9]G $\|swap\)"'. It's always a battle with these nested ones: one mistake, and you may search for 15 minutes where the "bug" is. – syntaxerror Dec 15 '14 at 17:54
3

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.

  • necropost away my man. answer approved 💯 – dps Jun 1 '18 at 5:35
1

What are you using server-side to print the parameters? It might well be an artefact from that method.

Both methods of quoting, .i.e. "{\"... and '{"... are ok.

Edit: I'll give you an example of the effect I'm hinting at:

% irb -r json
>> h = {"item" => "value with spaces"}    # (1)
=> {"item"=>"value with spaces"}          # (2)
>> h.to_json
=> "{\"item\":\"value with spaces\"}"     # (3)
>> puts(h.to_json)
{"item":"value with spaces"}              # (4)
=> nil

(1) defines a hashtable consisting of a single key with an associated value. irb shows you the result in (2), which is the hashtable again. Now when I look at the JSON representation of h, irb escapes the inner quotes in (3), while (4) shows that the backslashes are not "really" there.

So depending on how you print the parameters on the server, you might end up with the representation you showed us.

  • When I telnet directly into the server and do the http post by hand it goes through as expected. I don't believe the issue is on the server side. – Adam Davis May 20 '14 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.