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
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
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:
then in my case I issue:
Keeps the json more readable too.
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:
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:
To preserve quotes inside data, try to double-escape them like 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:
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.
Alternative solution: A More Userfriendly solution than command line:
For guys looking to stay with command-line i recommend cygwin:
Useful links:
I hope it helps someone because i spent all morning on this.
One more alternative cross-platform solution on powershell 6.2.3: