I use Ubuntu and installed cURL on it. I want to test my Spring REST application with cURL. I wrote my POST code at the Java side. However, I want to test it with cURL. I am
This worked for me for on Windows10
curl -d "{"""owner""":"""sasdasdasdasd"""}" -H "Content-Type: application/json" -X PUT http://localhost:8080/api/changeowner/CAR4
For Windows, having a single quote for the -d
value did not work for me, but it did work after changing to double quote. Also I needed to escape double quotes inside curly brackets.
That is, the following did not work:
curl -i -X POST -H "Content-Type: application/json" -d '{"key":"val"}' http://localhost:8080/appname/path
But the following worked:
curl -i -X POST -H "Content-Type: application/json" -d "{\"key\":\"val\"}" http://localhost:8080/appname/path
I am using the below format to test with a web server.
use -F 'json data'
Let's assume this JSON dict format:
{
'comment': {
'who':'some_one',
'desc' : 'get it'
}
}
curl -XPOST your_address/api -F comment='{"who":"some_one", "desc":"get it"}'
You need to set your content-type to application/json. But -d sends the Content-Type application/x-www-form-urlencoded
, which is not accepted on Spring's side.
Looking at the curl man page, I think you can use -H:
-H "Content-Type: application/json"
Full example:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/login
(-H
is short for --header
, -d
for --data
)
Note that -request POST
is optional if you use -d
, as the -d
flag implies a POST request.
On Windows, things are slightly different. See the comment thread.
Try to put your data in a file, say body.json
and then use
curl -H "Content-Type: application/json" --data @body.json http://localhost:8080/ui/webapp/conf
This worked for me:
curl -H "Content-Type: application/json" -X POST -d @./my_json_body.txt http://192.168.1.1/json