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;
}
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.
You might find resty useful: https://github.com/micha/resty
It's a wrapper round CURL which simplifies command line REST requests. You point it to your API endpoint, and it gives you PUT and POST commands. (Examples adapted from the homepage)
Also, it's often still necessary to add the Content Type headers. You can do this once, though, to set a default, of add config files per-method per-site: Setting default RESTY options
You need to set your content-type to application/json. But
-d
sends the Content-Typeapplication/x-www-form-urlencoded
, which is not accepted on Spring's side.Looking at the curl man page, I think you can use
-H
:Full example:
(
-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.
This worked for me:
I am using the below format to test with a web server.
Let's assume this JSON dict format:
Full example
Try to put your data in a file, say
body.json
and then use