Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

json - 如何通过Curl从终端/命令行发布JSON数据以测试Spring REST?(How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?)

I use Ubuntu and installed cURL on it.

(我使用Ubuntu并在其上安装了cURL 。)

I want to test my Spring REST application with cURL.

(我想用cURL测试我的Spring REST应用程序。)

I wrote my POST code at the Java side.

(我在Java端编写了POST代码。)

However, I want to test it with cURL.

(但是,我想用cURL对其进行测试。)

I am trying to post a JSON data.

(我正在尝试发布JSON数据。)

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

(Tomcat日志:“ POST / ui / webapp / conf / clear HTTP / 1.1” 415 1051)

What is the right format of the cURL command?

(cURL命令的正确格式是什么?)

This is my Java side PUT code (I have tested GET and DELETE and they work):

(这是我的Java端PUT代码(我已经测试了GET和DELETE并且它们可以工作):)

@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;
}
  ask by kamaci translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to set your content-type to application/json.

(您需要将内容类型设置为application / json。)

But -d sends the Content-Type application/x-www-form-urlencoded , which is not accepted on Spring's side.

(但是-d发送Content-Type application/x-www-form-urlencoded ,在Spring方面不接受。)

Looking at the curl man page , I think you can use -H :

(查看curl手册页 ,我认为您可以使用-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 )

(( -H代表--header-d代表--data ))

Note that -request POST is optional if you use -d , as the -d flag implies a POST request.

(请注意,如果使用-d ,则-request POST可选的 ,因为-d标志表示POST请求。)


On Windows, things are slightly different.

(在Windows上,情况略有不同。)

See the comment thread.

(请参阅评论主题。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...