Proper escaping of double quotations for curl post data?

26,633

Solution 1

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.

Solution 2

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.

Share:
26,633

Related videos on Youtube

Adam Davis
Author by

Adam Davis

RIP Stack Exchange. codidact.org topanswers.xyz

Updated on September 18, 2022

Comments

  • Adam Davis
    Adam Davis over 1 year

    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?

    • terdon
      terdon almost 10 years
      Have you tried curl -d '{"item":"value with spaces"}'?
    • Darth Hunterix
      Darth Hunterix almost 10 years
      terdon's advice should work, because single quotes are supposed to negate any special characters between them: howtogeek.com/howto/29980/…
    • Adam Davis
      Adam Davis almost 10 years
      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\"}"
    • syntaxerror
      syntaxerror over 9 years
      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.
  • Adam Davis
    Adam Davis almost 10 years
    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.
  • a2f0
    a2f0 almost 6 years
    necropost away my man. answer approved 💯