Include body request data in a POST request made via curl?

yaboy01 asked this question 21 days ago
yaboy01 21 days ago

I'm trying to make a POST request to an external API using curl.

How can I include JSON body data in that request?

That data would be available to the REST API via request.body.

3 suggested answers
1 reply
nick 15 days ago

Here's an example of how you do this:

curl -X POST -H  "Content-Type: application/json" -d '{"name": "Tacos", "calories": 200}' http://localhost:5000/foods/add-new-food

This curl command makes a POST request to a http://localhost:5000/foods/add-new-food API endpoint and includes the {"name": "Tacos", "calories": 200} object in the request available via request.body.

1 reply
looper003 15 days ago

For Windows, you may need to escape double quotes inside the request body object:

'{\"name\": \"Tacos\", \"calories\": 200}'

yaboy01 15 days ago · Edited
curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"email":"xyz", "password":"xyz"}' \
  • --header: this sets a HEADER for the request, telling the API that the request contains JSON data.
  • --request: sets the type of request (GET, PUT, POST, etc.).
  • --data: data for the POST request, available to the API via the request.body object.
0 replies
looper003 15 days ago

An example for you:

curl -d '{"item1":"value1", "item2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/your-api-endpoint

When executed, this will send a POST request with the object data included.

0 replies