CURL POST command line in WINDOWS RESTful service

My problem:

Running windows 7 and using an executable command-line tool to curl my localhost api for POST data returns an error, which seems to be very common.

The request is sent properly, except that the data did not reach the server, as it should force my REST service to insert data with zero values.

Which seems to be causing the error: imagine something like this

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

Return Data Result

 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, it turned out that the problem could not be the syntax used for the request, since it works with UNIX shells.

Perhaps you are using windows? It looks like a completely broken shell that does not correctly handle single quotes and double quotes. I just tried this command line and it worked perfectly on my Linux field. http://curl.haxx.se/mail/archive-2011-03/0066.html

I tried to work with those who "escaped", but it still did not 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 be encountering a JSON object sent to POST

+43
command-line windows post api curl
Aug 6 '12 at 19:01
source share
5 answers

I ran into the same problem on my win7 x64 laptop and was able to get it to work using the curl version designated by Win64 - Generic w SSL using a 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 is different from your second version of the escape, using double quotes around escaped and the value of the header parameter. Definitely prefer linux shell syntax more.

+81
Sep 19 '12 at 15:41
source share

Another alternative to the command line, which is easier than combating quotes, is to include json in the file and use the @ curl prefix, for example. in json.txt:

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

then in my case I give out:

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

Keeps json more readable.

+34
Jan 13 '14 at 9:14
source share

Alternative solution: a more convenient solution than on the command line:

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

For guys who want to stay on the command line, I recommend cygwin:

I ended up installing cygwin with CURL, which allows us to get the Sense of Linux - on Windows!

Using the Cygwin command line, these problems stopped and, most importantly, the query syntax used for 1. worked fine.

Useful links:

Where did I download curl for the windows command line?

For more information on how to install and make curl work with cygwin just go here

Hope this helps someone because I spent all morning on this.

+13
Aug 07 2018-12-12T00:
source share

At least for the binary version of Windows I tested ( Generic Win64 no-SSL binary , currently based on 7.33.0 ), you are subject to restrictions on how command line arguments are parsed . Xmas's answer describes the correct syntax in this parameter, which also works in a batch file. Using the above example:

 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, avoiding the use of escaped characters, which depends on which library is used for parsing the command line, is to have standard text in json format in a separate file:

 curl -i -X POST -H "Content-Type: application/json" -d "@body.json" http:localhost/path/to/api 
+2
May 13 '15 at 19:55
source share
  • Try using double quotes (") instead of single quotes (').
  • To preserve JSON format quotation marks, try doubling them ("").
  • To keep quotes inside the data, try to escape them twice (\\ "").

     curl ... -d "{""data1"": ""data1 goes here"", ""data2"": ""data2 goes here""}" curl ... -d "{""data"": ""data \\""abc\\"" goes here""}" 
0
Feb 18 '15 at 20:13
source share



All Articles