Home How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?
Reply: 0

How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

user8543
1#
user8543 Published in September 19, 2018, 3:50 am

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;
} 
share|improve this question
  • 2
    checkout the link for spring 3.2.0 post requests. – AmirHd Jun 5 '13 at 9:41
  • 3
    There is a nice post Using Curl For Ad Hoc Testing Of RESTful Microservices which covers this with multiple examples. – upitau Aug 28 '16 at 11:03

19 Answers 19

active oldest votes
up vote 3327 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.456882 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO