如何使用Curl从终端/命令行发布JSON数据以测试Spring REST? [英] How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?

查看:449
本文介绍了如何使用Curl从终端/命令行发布JSON数据以测试Spring REST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ubuntu和安装Curl就可以了。我想用Curl测试我的Spring REST应用程序。我在Java端写了我的POST代码。但是,我想用Curl测试它。我试图发布一个JSON数据。示例数据如下:

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

我使用这个命令:

  curl -i \ 
-H接受:application / json\
-HX-HTTP-方法覆盖:PUT\
-X POST -dvalue: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: :8080 / xx / xxx / xxxx

会返回此错误:

  HTTP / 1.1 415不支持的媒体类型
服务器:Apache-Coyote / 1.1
Content-Type:text / html; charset = utf- 8
Content-Length:1051
Date:Wed,24 Aug 2011 08:50:17 GMT


b $ b

错误描述为:


服务器拒绝此请求,因为请求实体的格式


Tomcat日志:
POST / ui / webapp / conf / clear HTTP / 1.1415 1051



有关Curl命令的正确格式的任何想法吗?



EDIT:



这是我的Java端PUT代码(我已经测试过GET和DELETE,

  @RequestMapping(method = RequestMethod.PUT)
public配置updateConfiguration(HttpServletResponse响应,@RequestBody配置配置){//考虑@有效标记
configuration.setName(PUT working);
// todo如果出现错误response.sendError(HttpServletResponse.SC_NOT_FOUND);
return configuration;
}


解决方案

类型到application / json。但是 -d 发送内容类型 application / x-www-form-urlencoded ,这在Spring的一方不接受。



查看 curl手册页,我认为您可以使用 -H

  -HContent-Type:application / json

$ b b

完整示例:

  curl -HContent-Type:application / json -d'{username:xyz,password:xyz}'http:// localhost:3000 / api / login 

-H - header 的缩写, -d for - data 。)






在Windows上,事情稍有不同。请参阅评论主题。


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 -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

(-H is short for --header, -d for --data.)


On Windows, things are slightly different. See the comment thread.

这篇关于如何使用Curl从终端/命令行发布JSON数据以测试Spring REST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆