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 trying to post a JSON data. 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
What is the right format of the cURL command?
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; }
To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.
To send the JSON with payload to the REST API endpoint, you need to enclose the JSON data in the body of the HTTP request and indicate the data type of the request body with the "Content-Type: application/json" request header.
To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.
You need to set your content-type to application/json. But -d
(or --data
) 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
(or --header
):
-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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With