50

How can I send a double quote char using curl.exe in the -d parameter. I don't want to URL encode the double quote. Since the -d data needs to be surrounded in double quotes, I cannot seem to get it to work.

Or is there another flag for curl.exe that tells it to use a files contents for the whole form post data?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
slolife
  • 19,520
  • 20
  • 78
  • 121

6 Answers6

59

My curl.exe works with this form:

-d "{\"param\":\"value\"}"

i.e. doublequotes around data, and doublequotes masked with backslash inside

Simon East
  • 55,742
  • 17
  • 139
  • 133
Alexander Chuprin
  • 599
  • 1
  • 4
  • 4
  • Not on Windows, in other Shells yes. – Nicholi Jul 02 '13 at 20:29
  • 8
    I tried in Windows 8.1 command prompt and escaping with backslash works fine! – user3523091 May 28 '14 at 04:11
  • 2
    This works OK for me in Windows 10 DOS Command shell. However, I also had to remove all the single quotes and replace with double quotes. For example here is the Hello World SendGrid v3 API call: curl --request POST --url https://api.sendgrid.com/v3/mail/send --header "Authorization: Bearer YOUR_API_KEY" --header "Content-Type: application/json" --data "{\"personalizations\": [{\"to\": [{\"email\": \"to_name@example.com\"}]}],\"from\": {\"email\": \"from_name@example.com\"},\"subject\": \"Hello, World!\",\"content\": [{\"type\": \"text/plain\", \"value\": \"Heya!\"}]}" – Richard Logwood Jan 01 '17 at 19:59
  • 1
    This method works well when passing JSON data on Windows OS. – Geek Stocks Jan 02 '17 at 08:57
  • 1
    This works great on Windows 10. Double quote the whole data payload, and then escape the inner quotes with a \ (whack) – Jonesome Reinstate Monica Mar 15 '18 at 15:05
  • I sure which CMD recognized single quote ' the same way as Bash. – Chloe Nov 06 '18 at 01:34
  • 1
    Interesting. That works for me in cmd.exe, but when I try it in PowerShell (using 'curl.exe' instead of 'curl' since 'curl' aliases to Invoke-WebRequest), I get an error message from the service I am querying. It's almost as if it escaped it one time too many along the way. Weeeeird. – Mike Loux Feb 14 '20 at 21:16
24

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.

You can also do this:

curl [...] -d @filename 

...which reads post data from a file called filename.

Google and/or man is your friend.

http://curl.haxx.se/docs/manpage.html

martona
  • 5,760
  • 1
  • 17
  • 20
  • 1
    The -d @filename was what I was looking for. When reading the manpage, I thought the @filename only worked for the -F argument. I didn't realize it worked with -d as well. – slolife Dec 22 '10 at 22:39
8

For the escaping of double quote question, I'm finding that tripling the doublequotes works from the shell:

curl -d {"""foo""":"""bar"""}

while doubling the doublequotes works from within a batch file:

curl -d {""foo"":""bar""}

Which is quite annoying for testing in the shell first.

Gaz
  • 4,064
  • 3
  • 32
  • 34
  • I don't think that will work if your JSON has spaces in it. Otherwise there is nothing holding the entire JSON together as a single argument. i.e. `{"""foo""":"""bar is the best"""}` – Chloe Nov 06 '18 at 01:36
8

You can surround the data with single quotes and use double quotes inside.

Example in PowerShell

curl.exe https://httpbin.org/anything `
    -H 'Content-Type: application/json' `
    -d '{ "this": "is proper json" }'.Replace('"', '""')

Please note that cURL is built into Windows 10, but PowerShell shadows it with an alias, so you have to use curl.exe

Gian Marco
  • 22,140
  • 8
  • 55
  • 44
Isac
  • 2,058
  • 16
  • 23
1

If you are doing this in Powershell, you will need to quote the whole text block with single quotes and still escape the quotes. i.e. -d '{\"param\":\"value\"}'

Tested on Win11 VSCode Powershell 7 terminal

-2

There is something called dollar slashy string.

def cmd = $/ curl -d '{"param":"value"}' -X POST https://example.com/service /$
sh cmd
Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71