如何使用 curl 发布原始身体数据? [英] How to post raw body data with curl?

查看:20
本文介绍了如何使用 curl 发布原始身体数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您将此作为副本发布之前;我已经尝试了围绕 SO 找到的许多建议.

到目前为止,我一直在使用邮递员将数据发布到 Java Web 服务.效果很好,如下所示:

我现在想使用 curl 来做同样的事情,所以我使用以下方法进行了尝试:

$ curl -X POST --data "这是原始数据" http://78.41.xx.xx:7778/$ curl -X POST --data-binary 这是原始数据" http://78.41.xx.xx:7778/$ curl -X POST --data "@/home/kramer65/afile.txt" http://78.41.xx.xx:7778/$ curl -X POST --data-binary "@/home/kramer65/afile.txt" http://78.41.xx.xx:7778/

不幸的是,所有这些都在接收方显示一个空的原始身体.

有人知道我在这里做错了什么吗?我的 curl 请求与邮递员请求有何不同?欢迎所有提示!

解决方案

curl 的 --data 默认会发送 Content-Type: application/x-www-form-urlencoded 在请求头中.但是,当使用 Postman 的 raw body 模式时,Postman 在请求头中发送 Content-Type: text/plain.

所以要实现与 Postman 相同的功能,请为 curl 指定 -H "Content-Type: text/plain":

curl -X POST -H "Content-Type: text/plain" --data "这是原始数据" http://78.41.xx.xx:7778/

请注意,如果您想观看 Postman 发送的完整请求,您可以为打包的应用程序启用调试.查看

Before you post this as a duplicate; I've tried many of the suggestions I found around SO.

So far I've been using postman to post data to a Java web service. That works great as follows:

I now want to do the same using curl, so I tried it using the following ways:

$ curl -X POST --data "this is raw data" http://78.41.xx.xx:7778/
$ curl -X POST --data-binary "this is raw data" http://78.41.xx.xx:7778/
$ curl -X POST --data "@/home/kramer65/afile.txt" http://78.41.xx.xx:7778/
$ curl -X POST --data-binary "@/home/kramer65/afile.txt" http://78.41.xx.xx:7778/

Unfortunately, all of those show an empty raw body on the receiving side.

Does anybody know what I'm doing wrong here? How is my curl request different from my postman request? All tips are welcome!

解决方案

curl's --data will by default send Content-Type: application/x-www-form-urlencoded in the request header. However, when using Postman's raw body mode, Postman sends Content-Type: text/plain in the request header.

So to achieve the same thing as Postman, specify -H "Content-Type: text/plain" for curl:

curl -X POST -H "Content-Type: text/plain" --data "this is raw data" http://78.41.xx.xx:7778/

Note that if you want to watch the full request sent by Postman, you can enable debugging for packed app. Check this link for all instructions. Then you can inspect the app (right-click in Postman) and view all requests sent from Postman in the network tab :

这篇关于如何使用 curl 发布原始身体数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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