ChatGPT解决这个技术问题 Extra ChatGPT

How to post raw body data with curl?

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:

https://i.stack.imgur.com/voBZy.png

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!


E
ErrCode

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 :

https://i.stack.imgur.com/k7qDN.png


It should be noted though that the body part is sent exactly the same. This just changes a header.