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
It worked for me using:
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":100}' http://localhost/api/postJsonReader.do
It was happily mapped to the Spring controller:
@RequestMapping(value = "/postJsonReader", method = RequestMethod.POST)
public @ResponseBody String processPostJsonData(@RequestBody IdOnly idOnly) throws Exception {
logger.debug("JsonReaderController hit! Reading JSON data!"+idOnly.getId());
return "JSON Received";
}
IdOnly
is a simple POJO with an id property.
I know, a lot has been answered to this question but wanted to share where I had the issue of:
curl -X POST http://your-server-end-point -H "Content-Type: application/json" -d @path-of-your-json-file.json
See, I did everything right, Only one thing - "@" I missed before the JSON file path.
I found one relevant go-to document on internet - https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
Hope that might help the few. thanks
As an example, create a JSON file, params.json, and add this content to it:
[
{
"environment": "Devel",
"description": "Machine for test, please do not delete!"
}
]
Then you run this command:
curl -v -H "Content-Type: application/json" -X POST --data @params.json -u your_username:your_password http://localhost:8000/env/add_server
You could also put your JSON content in a file and pass it to curl using the --upload-file
option via standard input, like this:
echo 'my.awesome.json.function({"do" : "whatever"})' | curl -X POST "http://url" -T -
You can use Postman with its intuitive GUI to assemble your cURL
command.
Code
cURL
from the drop-down listcURL
commandNote: There are several options for automated request generation in the drop-down list, which is why I thought my post was neccessary in the first place.
This worked well for me, additionally using BASIC authentication:
curl -v --proxy '' --basic -u Administrator:password -X POST -H "Content-Type: application/json"
--data-binary '{"value":"30","type":"Tip 3","targetModule":"Target 3","configurationGroup":null,"name":"Configuration Deneme 3","description":null,"identity":"Configuration Deneme 3","version":0,"systemId":3,"active":true}'
http://httpbin.org/post
Of course, you should never use BASIC authentication without SSL and a checked certificate.
I ran into this again today, using Cygwin's cURL 7.49.1 for Windows... And when using --data
or --data-binary
with a JSON argument, cURL got confused and would interpret the {}
in the JSON as a URL template. Adding a -g
argument to turn off cURL globbing fixed that.
See also Passing a URL with brackets to curl.