如何使用curl发布原始身体数据?

问题描述:

在将其发布为重复副本之前;我尝试了很多关于SO的建议.

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

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

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

我现在想使用curl进行相同的操作,因此我尝试使用以下方式进行尝试:

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的--data默认情况下将在请求标头中发送Content-Type: application/x-www-form-urlencoded.但是,当使用Postman的raw正文模式时,Postman在请求标头中发送Content-Type: text/plain.

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.

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

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/

请注意,如果您想观看Postman发送的完整请求,则可以启用打包应用程序的调试.查看此链接以获取所有说明.然后,您可以检查该应用程序(在Postman中单击鼠标右键),并在network标签中查看从Postman发送的所有请求:

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 :