cURL POST command line on WINDOWS RESTful service
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.
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.
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
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.