post JSON with cURL

The problem of post JSON with cURL

In this blog post, we will learn how we can sort a particular error of cURL. One example of a problem is that users try to test their application with the help of cURL. To post JSON with cURL user needs to run the following command which is shown below:

 

 

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

 

 

While running this command one can get the error appearing on their screen. The error seems to appear as follows:

 

 

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 meaning behind this error is also shown in the picture shown below:

 

 

The java code is also shown below which the user has written while testing the application on the cURL platform.

 

 

@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;

}

 

 

Solution

One of the solutions for the above problem can be that the content-type setting should be changed to application/JSON. However, -d (or —data) transmits the unaccepted Content-Type application/x-www-form-urlencoded to Spring. For the purpose of the header, one can use the following command:

 

 

-H "Content-Type: application/json"

 

 

An example of the same is shown below:

 

 

Another command is for putting the data in the JSON file. The command is shown below:

 

 

curl -H "Content-Type: application/json" --data @body.json http://localhost:8080/ui/webapp/conf

 

It is found that using a single quotation for the -d value in Windows did not work, but using a double quote made a difference. Additionally, double quotations inside curly brackets must be escaped.

 

 

Also Read: rmarkdown figure auto-adjust HTML

 

Share this post

2 thoughts on “post JSON with cURL

Leave a Reply

Your email address will not be published. Required fields are marked *