为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用Curl将JSON数据从终端/命令行发布到TestSpringREST?

如何使用Curl将JSON数据从终端/命令行发布到TestSpringREST?

慕莱坞森 2019-06-18 15:40:03
如何使用Curl将JSON数据从终端/命令行发布到TestSpringREST?我使用Ubuntu并在上面安装了Curl。我想用curl测试我的SpringREST应用程序。我在Java端写了我的邮政编码。但是,我想用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 "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它返回此错误: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 GMT错误描述如下:服务器拒绝此请求,因为请求实体的格式不受请求方法()的请求资源的支持。Tomcat日志:“post/ui/webapp/conf/ClearHTTP/1.1”415 1051Curl命令的正确格式是什么?这是我的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;}
查看完整描述

3 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

您需要将内容类型设置为application/json。但-d发送内容类型application/x-www-form-urlencoded,这在春的一方是不被接受的。

看着卷曲手册页,我想你可以用-H:

-H "Content-Type: application/json"

完整的例子:

curl --header "Content-Type: application/json" \  --request POST \  --data '{"username":"xyz","password":"xyz"}' \
  http://localhost:3000/api/login

(-H是缩写--header-d--data)

请注意-request POST任选如果你用-d,作为-d标志意味着一个POST请求。


在Windows上,情况略有不同。请参见注释线程。


查看完整回答
反对 回复 2019-06-18
?
DIEA

TA贡献1820条经验 获得超2个赞

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

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


查看完整回答
反对 回复 2019-06-18
  • 3 回答
  • 0 关注
  • 805 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号