[windows] cURL POST command line on WINDOWS RESTful service

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

This question is related to windows api post curl command-line

The answer is


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.


  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""}"
    

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

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

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.


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.


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

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to api

I am receiving warning in Facebook Application using PHP SDK Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file Failed to load resource: the server responded with a status of 404 (Not Found) css Call another rest api from my server in Spring-Boot How to send custom headers with requests in Swagger UI? This page didn't load Google Maps correctly. See the JavaScript console for technical details How can I send a Firebase Cloud Messaging notification without use the Firebase Console? Allow Access-Control-Allow-Origin header using HTML5 fetch API How to send an HTTP request with a header parameter? Laravel 5.1 API Enable Cors

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time

Examples related to curl

What is the incentive for curl to release the library for free? curl: (35) error:1408F10B:SSL routines:ssl3_get_record:wrong version number Converting a POSTMAN request to Curl git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to post raw body data with curl? Curl : connection refused How to use the curl command in PowerShell? Curl to return http status code along with the response How to install php-curl in Ubuntu 16.04 curl: (35) SSL connect error

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)