How to make a Curl POST call in Windows?

I am trying to issue the following command under Windows 10:

D:\>curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' 

Unfortunately, it produces numerous errors:

curl: (6) Could not resolve host: firstName
curl: (7) Failed to connect to port 80: Connection refused
curl: (6) Could not resolve host: Frodo,
curl: (6) Could not resolve host: lastName
curl: (7) Failed to connect to port 80: Connection refused
curl: (6) Could not resolve host: Baggins
curl: (3) [globbing] unmatched close brace/bracket in column 1

Apparently, it does not understand the syntax.

Why is this, and how can this be fixed?

D:\>curl --version
curl 7.46.0 (x86_64-pc-win32) libcurl/7.46.0 OpenSSL/1.0.2e zlib/1.2.8 WinIDN libssh2/1.6.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL libz
1

5 Answers

Another option is to mask doublequotes with backslash like this:

curl -i -X POST -H "Content-Type:application/json" -d "{\"firstName\": \"Frodo\", \"lastName\" : \"Baggins\" }" 
1

It looks like you're using cmd.exe. Command Prompt's character escaping rules are both archaic and awful. I recommend using Powershell instead; it uses rules much more similar to those of bash on other *nix shells (though not identical, notably because it uses ` (backtick) as the escape character instead of backslash).

Here's the command in Powershell on my system:

& 'C:\Program Files\Git\mingw64\bin\curl' -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' 

The leading & is required because the path to the program is a quoted string. I had to specify the path because I don't have a curl.exe in my Windows PATH. However, I could just escape the space in "Program Files":

C:\Program` Files\Git\mingw64\bin\curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins" }' 

Single and double quotes otherwise work as you're using them, with the ' delimiting the start of a string and the " appearing just as literal characters inside it.

Note that you do have to provide the path to a curl executable, or at least specify curl.exe; curl by itself is a Powershell alias for the Invoke-WebRequest cmdlet, which can do most of what the cURL program can do but has very different argument syntax.

Also, while you can invoke Powershell from cmd using powershell -c <command>, that wouldn't really help here because you'd have to escape the string using cmd's silly syntax anyhow.


Another option is just to use the Windows Subsystem for Linux (WSL), which lets you run Linux programs (including the default Ubuntu versions of bash and curl) directly on Windows, no VM or rebooting needed. Full details about this can be found at , but the short version is try running bash (or bash.exe) from any Command Prompt or Powershell window, and it will install the Linux subsystem or at least tell you how.

2

The problem is your -d argument with double quotes as the argument. You need to surround it in quotes and escape the double quotes inside.

You should be able to do:

curl -i -X POST -H "Content-Type:application/json" -d "{ ^"firstName^" : ^"Frodo^", ^"lastName^" : ^"Baggins^" }" 

Personally, I'd stick them in a file, then use -d @filename for clarity.

Another workaround in Windows is to use bash from Cygwin. I had the same problem, with bash it works for me.

For Windows 10, the built-in Linux application support might handle the single quotes as well.

4

Windows curl doesn't aware of single quotes. Therefore, you can use double quotes for your payload and escape any other double quotes inside that payload. That comes out like below:

curl -d "{\"firstName\": \"Frodo\", \"lastName\" : \"Baggins\" }" -H "Content-Type:application/json" -X POST 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like