Curl - POST to a REST API

by
Jeremy Canfield |
Updated: November 25 2024
| Curl articles
The curl command with the -d or --data option is used to send a POST request to a server.
For example, let's says http://www.example.com/api/v1 will return success when the following key value pairs are in the POST.
- foo = Hello
- bar = World
By default, when the -d or --data option is used, curl will make a POST request, thus there is no need to include -X POST or --request POST.
curl \
--request POST \
--url http://www.example.com/api/v1 \
--data "foo=bar"
And here is an example of how to POST JSON.
curl \
--request POST \
--url http://www.example.com/api/v1 \
--data '{ "foo": "bar" }' \
-v
The -v (verbose) flag can be used to see the Content-Type the REST API expects.
~]# curl --request POST --url http://www.example.com/api/v1 --data '{ "foo": "bar" }' -v
> content-type: application/x-www-form-urlencoded
< content-type: application/json
If the REST API expects content-type: application/json, the -H or --header option can be used to set the Content-Type to application/json.
curl \
--request POST \
--url http://www.example.com/api/v1 \
--header 'content-type: application/json' \
--data '{ "foo": "bar" }'
Did you find this article helpful?
If so, consider buying me a coffee over at