slolife
slolife

Reputation: 19870

How to send double quote in -d parameter for curl.exe?

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?

Upvotes: 54

Views: 96267

Answers (7)

Assombrance Andreson
Assombrance Andreson

Reputation: 431

Old question but I liked none of the solutions (actually the -d @filename would have been fine but it wasn't working for me). So after reading the doc (https://curl.se/docs/manpage.html) here is what I ended up using:

echo '{ "a": 1, "b": 2 }' | curl -H "Content-Type: application/json" -d "@-" http://localhost:8080

Upvotes: 0

Nathan Daniels
Nathan Daniels

Reputation: 21

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

Upvotes: 2

Isac
Isac

Reputation: 2068

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

Upvotes: 9

Anand Rockzz
Anand Rockzz

Reputation: 6658

There is something called dollar slashy string.

def cmd = $/ curl -d '{"param":"value"}' -X POST https://example.com/service /$
sh cmd

Upvotes: -2

Alexander Chuprin
Alexander Chuprin

Reputation: 639

My curl.exe works with this form:

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

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

Upvotes: 63

Gaz
Gaz

Reputation: 4154

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.

Upvotes: 9

martona
martona

Reputation: 5914

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

Upvotes: 24

Related Questions