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;
}
This worked well for me.
Where,
-X
Means the http verb.--data
Means the data you want to send.A bit late to the party, but I don't see this posted, so here goes, you could also put your json in a file and pass it to curl using --file-upload option via standard input, like this:
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:
But the following worked:
I just run into the same problem. I could solve it by specifying
You can pass the extension of the format you want as the end of the url. like http://localhost:8080/xx/xxx/xxxx.json
or
http://localhost:8080/xx/xxx/xxxx.xml
Note: you need to add jackson and jaxb maven dependencies in your pom.
It worked for me using:
It was happily mapped to the Spring controller:
IdOnly
is a simple POJO with an id property.