标签云

微信群

扫码加入我们

WeChat QR Code

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/xxxxIt returns this error:HTTP/1.1 415 Unsupported Media TypeServer: Apache-Coyote/1.1Content-Type: text/html;charset=utf-8Content-Length: 1051Date: Wed, 24 Aug 2011 08:50:17 GMTThe 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 1051What 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 tagconfiguration.setName("PUT worked");//todo If error occurs response.sendError(HttpServletResponse.SC_NOT_FOUND);return configuration;}


checkout the link for spring 3.2.0 post requests.

2019年06月26日55分42秒

There is a nice post Using Curl For Ad Hoc Testing Of RESTful Microservices which covers this with multiple examples.

2019年06月26日55分42秒

I removed the -d option from command but still same.

2019年06月26日55分42秒

For windows, single quotes around json did not work and I ended up escaping double quotes. curl -X POST -H "Content-Type: application/json" -d "{ \"key1\": \"value1\" }" http://localhost:3000/api/method

2019年06月26日55分42秒

For me under Windows I needed to escape quotes using quotes in this format "{ """key1""": """value1""" }". Also this answer: stackoverflow.com/questions/18314796/…

2019年06月26日55分42秒

chodorowicz that's awful! I only know that syntax from VB!

2019年06月26日55分42秒

ostrokach sorry it wasted your time. syntax worked fine for me on OSX when I posted it (haven't retried). Guess it is/was just a platform difference. I imagine the upvotes are from people that it helped.

2019年06月26日55分42秒

It returns 404 error

2019年06月26日55分42秒

You probably should use the --data-binary option instead of --data. One would expect that the client sends the data as-is, but --data strips CR und LF from the input.

2019年06月26日55分42秒

Using cUrl with inline json Strings seems to be a nightmare. There's the need to scape the double quote character. Going with a file like this is nicer.

2019年06月26日55分42秒

It's important to add an character before the name of the file, otherwise it won't work. I just spent 20 minutes banging my head at this crap...

2019年06月26日55分42秒

I was going to upvote this answer, but its current score is 404…

2019年06月26日55分42秒

FYI - looks like you're missing a closing double quote around the json body

2019年06月26日55分42秒

For me on Windows, the " around the data does not work, no quotes works instead

2019年06月26日55分42秒

If you're using PowerShell, see this answer.

2019年06月26日55分42秒

The -X POST is redundant in this example

2019年06月26日55分42秒

Didn't realize that feature was included in Postman.Thanks for pointing it out!

2019年06月26日55分42秒

Thanks a lot forever-LA it was a life saver tip for me

2019年06月26日55分42秒

That would only work if the server-side accepted the URLS with those extensions.

2019年06月26日55分42秒

Right, this will work with server side.

2019年06月26日55分42秒