How to make a POST request with curl

The curl is a command-line utility that enables the user to transfer data using any standard protocol. It is functional on all key operating systems including Linux. The curl command is mainly used to test the APIs (Application programming interface) by sending the post request and comes pre-installed on many Linux distributions. However, we need to install the curl command manually in Ubuntu 20.04 (LTS) long-term support.

The Ubuntu 20.04 LTS is being used for the demonstration. 

Install curl on Ubuntu 20.04

Type the below-given command to install curl in Ubuntu 20.04:

 $ sudo apt install curl

Note: You can skip this step if the curl command is already installed on your system.

Once the curl command is installed, verify the installed version using the command:

$ curl --version

Make POST request using curl

We will use the HTTP post method to send data to the remote server.

Following is the simple form of the curl command to send a post request:

$ curl -X POST [options] [the URL of server]

The -X option is used to describe the HTTP method. The curl command employs the Get method as the default HTTP method. However, to make a POST request, we need to specify POST with the -X option. 

A general post request can be made as follows:

$ curl –X POST https://example.com/

Send additional fields with the POST request

Normally, a post request is made by an HTML form. 

I have created a demo API that accepts the user name and age. Let’s make a post request and send the name and age values. The –d option is used with the curl command to send the additional field’s data to the remote server. 

$ curl -X POST -d 'name=kamran&age=24' https://curlliuxexample.000webhostapp.com/index.php

Instead of the –d option, we can also use the –F option to send the additional fields with the post request as follows:

curl -X POST -F 'name=kamran' -F 'age=24' https://curlliuxexample.000webhostapp.com/index.php
link cmd

While using the –F option, we cannot merge the data with & operator. The –d option uses the application/x-www-form-urlencoded Content-Type while send the additional field’s data to the server, whereas the-F option uses the multipart/form-data Content-Type.

Specify the Header or Content-type in POST request

The Header or Content-type can be specified in a POST request using -H option. Let’s set the Content-Type to application/json and sends the JSON data using the command:

$ curl -X POST -H "Content-Type: application/json" -d '{"name": "kamran", "age": "24"}' https://example.com

Upload the file(s) with the curl command

The files can be uploaded using the curl command. To upload a file with the curl command, just add ‘@’ before the location of the file: 

$ curl -X POST -F 'image=@/home/Downloads/mypic.jpg' http://example.com 

In the afore-mentioned command, I am uploading an image file using the curl command.

Conclusion

This post describes the usage of the curl command. Using the curl command, we can send the additional field data and upload the files to the server.

Subscribe to Our Newsletter
Join our Newsletter and get the latest Linux news and tips
in your inbox for FREE.