81

My problem: Using the command line tool to curl my localhost server while sending some data along with my POST request is not working.

What seems to be causing the error: Imagine something like this

  1. curl -i -X POST -H 'Content-Type: application/json' -d '{"data1": "data goes here", "data2": "data2 goes here"}' http:localhost/path/to/api

Result of the returning data

curl: (6) Could not resolve host: application; No data record of requested type
curl: (6) Could not resolve host: data goes here,; No data record of requested type
curl: (6) Could not resolve host: data2; No data record of requested type
curl: (3) [globbing] unmatched close brace/bracket at pos 16

After some searching i figured out that problem couldn't be the sintax used for the request since it works on UNIX shells.

Are you possibly using Windows? That so looks like a completely broken shell that doesn't properly deal with single-quotes vs double-quotes. I just tried that command line and it worked fine on my linux box. http://curl.haxx.se/mail/archive-2011-03/0066.html

I tried to work around with those " escaping it \" but it still didn't work

2.

curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api

3.

curl -i -X POST -H 'Content-Type: application/json' -d '{\"data1\": \"data goes here\", \"data2\": \"data2 goes here\"}' http: //localhost/path/to/api

So i gave up. Windows seems to messing up with the JSON object sent on POST

Lothre1
  • 3,523
  • 7
  • 43
  • 64

8 Answers8

149

I ran into the same issue on my win7 x64 laptop and was able to get it working using the curl release that is labeled Win64 - Generic w SSL by using the very similar command line format:

C:\Projects\curl-7.23.1-win64-ssl-sspi>curl -H "Content-Type: application/json" -X POST http://localhost/someapi -d "{\"Name\":\"Test Value\"}"

Which only differs from your 2nd escape version by using double-quotes around the escaped ones and the header parameter value. Definitely prefer the linux shell syntax more.

user1683523
  • 1,644
  • 2
  • 10
  • 4
  • 1
    Even in Windows 10, the single quotes don't seem to work. I had to do the double quotes with escaped double quotes for it to work. – Nelson Dec 06 '21 at 00:58
59

Another Alternative for the command line that is easier than fighting with quotation marks is to put the json into a file, and use the @ prefix of curl parameters, e.g. with the following in json.txt:

{ "syncheader" : {
    "servertimesync" : "20131126121749",
    "deviceid" : "testDevice"
  }
}

then in my case I issue:

curl localhost:9000/sync -H "Content-type:application/json" -X POST -d @json.txt

Keeps the json more readable too.

wwkudu
  • 2,778
  • 3
  • 28
  • 41
  • 1
    This one saved me! Also note that if the JSON you want to POST has command line variables in it you can use `sed` to replace them. – knueser Dec 06 '19 at 14:28
18

Alternative solution: A More Userfriendly solution than command line:

If you are looking for a user friendly way to send and request data using HTTP Methods other than simple GET's probably you are looking for a chrome extention just like this one http://goo.gl/rVW22f called AVANCED REST CLIENT

For guys looking to stay with command-line i recommend cygwin:

I ended up installing cygwin with CURL which allow us to Get that Linux feeling - on Windows!

Using Cygwin command line this issues have stopped and most important, the request syntax used on 1. worked fine.

Useful links:

Where i was downloading the curl for windows command line?

For more information on how to install and get curl working with cygwin just go here

I hope it helps someone because i spent all morning on this.

Community
  • 1
  • 1
Lothre1
  • 3,523
  • 7
  • 43
  • 64
  • 2
    This should not be the accepted answer. The OP did not ask for recommendations for a web based REST client or and alternative command line client. They asked for help getting the escaping correct for curl, which was answered by user1683523. – Craig B May 29 '15 at 19:46
  • 3
    But it's a better answer for many folks who are asking that question. – RickAndMSFT Jun 23 '16 at 23:10
  • Yeah, I agree. This solved my problem, even if it was in a different way than I intended. curl just wouldn't work for me, no matter how I escaped the quotes. I tried single quotes around json and double inside, double everywhere with a slash to escape, double-double quote. Nothing worked. The google extension did send it the way it's supposed to be sent. Sometimes a fix for a tool not doing what it's supposed to is to use a different tool. – Creature Feb 13 '19 at 19:48
13

At least for the Windows binary version I tested, (the Generic Win64 no-SSL binary, currently based on 7.33.0), you are subject to limitations in how the command line arguments are being parsed. The answer by xmas describes the correct syntax in that setting, which also works in a batch file. Using the example provided:

curl -i -X POST -H "Content-Type: application/json" -d "{""data1"":""data goes here"",""data2"":""data2 goes here""}" http:localhost/path/to/api

A cleaner alternative to avoid having to deal with escaped characters, which is dependent upon whatever library is used to parse the command line, is to have your standard json format text in a separate file:

curl -i -X POST -H "Content-Type: application/json" -d "@body.json" http:localhost/path/to/api
mysteryegg
  • 491
  • 5
  • 11
  • 1
    Thanks. It didn't use to be important but now with either Windows 10 or my installed curl version, I have to now put quotes around my json file argument after the -d or else the line endings will mess it up. – joezen777 Mar 23 '16 at 18:52
7
  1. Try to use double quotes (") instead of single ones (').
  2. To preserve JSON format quotes, try doubling them ("").
  3. To preserve quotes inside data, try to double-escape them like this (\\"").

    curl ... -d "{""data1"": ""data1 goes here"", ""data2"": ""data2 goes here""}"
    curl ... -d "{""data"": ""data \\""abc\\"" goes here""}"
    
xmas
  • 71
  • 1
  • 1
  • Using Win10, cmd.exe, out of the box, this answer worked for me. I had to double up on my double-quotes within the Json object, and put the whole object in quotes. Thanks @xmas ! – Steve Hibbert Sep 20 '18 at 14:10
  • 1
    This works fine but it is just another reason to hate windows. – Paul Rooney Feb 02 '22 at 11:15
4

One more alternative cross-platform solution on powershell 6.2.3:

$headers = @{
    'Authorization' = 'Token 12d119ad48f9b70ed53846f9e3d051dc31afab27'
}

$body = @"
{
    "value":"3.92.0", 
    "product":"847"
}
"@


$params = @{
    Uri         = 'http://local.vcs:9999/api/v1/version/'
    Headers     = $headers
    Method      = 'POST'
    Body        = $body
    ContentType = 'application/json'

}

Invoke-RestMethod @params
gek
  • 524
  • 6
  • 18
2

The double quotes in JSON need to be escaped, and the whole JSON needs to be enclosed in double quotes. To do this without manual escaping or using an extra file for the JSON, you can use CMD's ability to replace strings in variables to escape " to \":

set json={"data1": "data goes here", "data2": "data2 goes here"}
curl -i -X POST -H 'Content-Type: application/json' -d "%json:"=\"%" http://localhost/path/to/api
stevek_mcc
  • 360
  • 4
  • 9
1

We can use below Curl command in Windows Command prompt to send the request. Use the Curl command below, replace single quote with double quotes, remove quotes where they are not there in below format and use the ^ symbol.

curl http://localhost:7101/module/url ^
  -d @D:/request.xml ^
  -H "Content-Type: text/xml" ^
  -H "SOAPAction: process" ^
  -H "Authorization: Basic xyz" ^
  -X POST
ib.
  • 27,830
  • 11
  • 80
  • 100