January Sales! - 25% off premium subscriptions when you use coupon code: JAN2022 - View Pricing

Sending HTTP POST Requests With curl

Elliot Forbes Elliot Forbes ⏰ 1 Minutes 📅 Dec 13, 2021

In this snippet, we are going to be taking a look at how we can send HTTP POST requests using the curl command line tool. We’ll also be looking at how we can specify a request body as well as the Content-Type headers that you typically send alongside POST requests.

Sending a HTTP POST Request with Curl

The curl tool takes in a flag -X which allows you to specify the HTTP verb you wish to use for the request. In this case, we want to specify curl -X POST to signifiy this is an HTTP POST request.

$ curl -X POST http://api:port/api/v1/path 

Adding A Post Body

Now, typically we are sending data alongside the POST request to our API endpoint. In order to do this, we can use the -d flag in order to specify the data we wish to send:

$ curl -d '{"hello": "world"}' -X POST http://api:port/api/v1/path

Specifying Headers With Curl

We can specify headers in our curl requests using the -H like this:

$ curl -d '{"hello": "world"}' -H 'Content-Type: application/json' -X POST http://api:port/api/v1/path