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

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

You need to set your content-type to application/json. But -d 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 :

-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.


尝试把你的数据放在一个文件中,比如body.json ,然后使用

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

You might find resty useful: https://github.com/micha/resty

It's a wrapper round CURL which simplifies command line REST requests. You point it to your API endpoint, and it gives you PUT and POST commands. (Examples adapted from the homepage)

$ resty http://127.0.0.1:8080/data #Sets up resty to point at your endpoing
$ GET /blogs.json                  #Gets http://127.0.0.1:8080/data/blogs.json
                                   #Put JSON
$ PUT /blogs/2.json '{"id" : 2, "title" : "updated post", "body" : "This is the new."}'
                                   # POST JSON from a file
$ POST /blogs/5.json < /tmp/blog.json

Also, it's often still necessary to add the Content Type headers. You can do this once, though, to set a default, of add config files per-method per-site: Setting default RESTY options

链接地址: http://www.djcxy.com/p/136.html

上一篇: 为什么我必须在更改提交消息后强制推送?

下一篇: 如何使用终端/命令行中的Curl来发布JSON数据以测试Spring REST?