Friday, September 5, 2014

"Expecting to find valid JSON in request body..." curl for Windows

I was testing out curl command to access Openstack API under Windows environment and whenever i used the similar command which is working in Linux, i always encounter the following error message when do it on Windows.

The command and the error message:


curl -i --insecure https:/10.10.2.9:5000/v2.0/tokens -X POST -H "Content-type:application/json" -H "User-Agent:python-keystoneclient" -d '{"auth":{"tenantName":"testTenant","passwordCredentials":{"username":"testUser","password":"passsword"}}}'

HTTP/1.1 400 Bad Request
Date: Fri, 05 Sep 2014 08:46:13 GMT
Server: Apache/2.4.7 (Ubuntu)
Vary: X-Auth-Token
Content-Type: application/json
Content-Length: 244
Connection: close

{"error": {"message": "Expecting to find valid JSON in request body. The server could not comply with the request since it is either malformed or otherwise incorrect. The client is assumed to be in error.", "code": 400, "title": "Bad Request"}}


Until i almost gave up on this problem, i found this link which have the following notes:

"Note: cURL commands that contain single quotes ( ' ) will fail on Windows. When possible, use double quotes ( " ) in place of single quotes. If a command requires both single quotes and double quotes, escape the double quotes with a backslash (for example: \" ) and replace the single quotes with double quotes."

By inserting escape character and replacing single quote with double quotes, the command can be completed successfully.


curl -i --insecure https://10.10.2.9:5000/v2.0/tokens -X POST -H "Content-Type:application/json" -H "User-Agent:python-keystoneclient" -d "{\"auth\":{\"tenantName\":\"testTenant\",\"passwordCredentials\":{\"username\":\"testUser\",\"password\":\"password\"}}}"

HTTP/1.1 200 OK
Date: Fri, 05 Sep 2014 08:58:42 GMT
Server: Apache/2.4.7 (Ubuntu)
Vary: X-Auth-Token
Content-Type: application/json
Content-Length: 2200

......

2 comments:

Chris said...

Thanks for the info, saved my day!

Unknown said...

How ridiculous that none of the documentation I read on curl including the 200 page manual explained this simple incompatibility when using curl with Windows-os...

Thanks for the info, helped me a lot!