15

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 Jan 7, 2019 at 20:05

5 Answers 5

25

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
0
7

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.

2
  • 3
    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, 2019 at 13:26
  • I get this: {"statusCode":400,"error":"Bad Request","message":"Unexpected token u in JSON at position 2"} This is my command: curl.exe -X POST -H "Content-Type: application/json" -d '{ "username": "Code", "password": "Witcher" }' localhost:3000/login What's the issue? Feb 24, 2022 at 16:00
1

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.

0

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
1
  • You're confusing an operating system (Windows) with a shell (Command Prompt a.k.a. CMD). Powershell, and Unix shells such as bash, support multiple quote types just fine even when run on Windows.
    – CBHacking
    Nov 5, 2022 at 10:17
-1

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, 2017 at 16:44
  • @CBHacking, the question is not tagged Windows 10, is it?
    – Arjan
    Apr 22, 2017 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, 2017 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, 2017 at 18:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .