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" }' http://localhost:8080/people

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

Solution 1:

Another option is to mask doublequotes with backslash like this:

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

Solution 2:

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" }' http://localhost:8080/people

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" }' http://localhost:8080/people

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 https://msdn.microsoft.com/en-us/commandline/wsl/about, 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.