9

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
1
  • 2
    You can always try using git bash on windows to feel more unix comfortable on windows – user2736334 Jan 7 '19 at 20:05
17

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
1
  • 1
    please guys have a look at this solution it works, thanks man :) – Codesingh Jun 25 '17 at 12:37
6

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.

1
  • 2
    For newcomers to the thread, Curl been included in Windows 10 for a while now. source: techcommunity.microsoft.com/t5/Containers/… To check if your Windows 10 has it, run the following from PowerShell or CMD: C:\Windows\System32\curl.exe --help – Bewc Aug 20 '19 at 13:26
0

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

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

-2

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
  • 4
    Why in the world would you use Cygwin when Win10 has fully Linux application support? May as well use Wine on Windows! – CBHacking Apr 22 '17 at 16:44
  • @CBHacking, the question is not tagged Windows 10, is it? – Arjan Apr 22 '17 at 16:46
  • 2
    @Arjan It should be (and now is) as the OP said in the question "I am trying to issue the following command under Windows 10" – DavidPostill Apr 22 '17 at 17:01
  • In that case, and if the built-in Windows 10 Linux support also supports the single quotes like Cygwin, then I'd prefer an edit for the answer, @David. – Arjan Apr 22 '17 at 18:41
-2

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.