I use Ubuntu and installed Curl on it. I want to test my Spring REST application with Curl. I wrote my POST code at Java side. However, I want to test it with Curl. I am trying to post a JSON data. An example data is like this:
{"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}
I use this command:
curl -i \
-H "Accept: application/json" \
-H "X-HTTP-Method-Override: PUT" \
-X POST -d "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://localhost:8080/xx/xxx/xxxx
It returns this error:
HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1051
Date: Wed, 24 Aug 2011 08:50:17 GMT
The error description is this:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
Tomcat log: "POST /ui/webapp/conf/clear HTTP/1.1" 415 1051
Any ideas about the right format of the Curl command?
EDIT:
This is my Java side PUT code (I have tested GET and DELETE and they work)
@RequestMapping(method = RequestMethod.PUT)
public Configuration updateConfiguration(HttpServletResponse response, @RequestBody Configuration configuration) { //consider @Valid tag
configuration.setName("PUT worked");
//todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND);
return configuration;
}
As an example, create a JSON file, params.json, and add this content to it:
Then you run this command:
This worked well for me, additionally using BASIC authentication:
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.
HTTPie is a recommended alternative to
curl
because you can do justIt speaks JSON by default and will handle both setting the necessary header for you as well encoding data as valid JSON. There is also:
for headers, and
for query string parameters. If you have a large chunk of data, you can also read it from a file have it be JSON encoded:
Using CURL Windows, try this:
If you're testing a lot of JSON send/responses against a RESTful interface, you may want to check out the Postman plug-in for Chrome (which allows you to manually define web service tests) and its Node.js-based Newman command-line companion (which allows you to automate tests against "collections" of Postman tests.) Both free and open!