How to Post Raw Body Data with Curl

How to post raw body data with curl?

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 :

Sample Image

How to use variables inside data-raw json string in a curl command

Just escape the double quotes:

--data-raw "{
\"ipAddress\": \"$ip_address\",
\"userId\": \"$user_id\"
}"

CURL command with --data-raw JSON, Unexpected character error

Solution:

 --data-raw '{"query": "{applications(limit: 2) {nodes {name}}}" }'

How do I POST JSON data with cURL?

You need to set your content-type to application/json. But -d (or --data) 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 (or --header):

-H "Content-Type: application/json"

Full example:

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

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

Note that -request POST is optional if you use -d, as the -d flag implies a POST request.


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

How to make curl request by passing raw data to post method?

Instead of body pass json

$response = $this->httpClient->request('POST', $url, [
'headers' => [
'x-auth-token' => $authToken,
'x-auth-key' => $authKey
],
'body' => [
"1",
"2",
"3",
"4",
]
]);

Send request to cURL with post data sourced from a file

You're looking for the --data-binary argument:

curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "@path/to/file"

In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an @ symbol, so curl knows to read from a file.

POST with arbitrary body using curl

you can send arbitrary request body with --data

curl -X POST /api/route --data ' could be anything ' --header 'Content-Type: application/json

if you send json I would suggest to add the json content headers



Related Topics



Leave a reply



Submit