How To make a POST request using cURL?

Certainly, you can make a post request using cURL by simply adding POST method. That is to say, cURL is an shorten form of Client URL. Meanwhile, using cURL, you can download or upload data using the supported protocols including HTTP, HTTPS, SCP, FTP and SFTP. However, in this article, I will explain how to make a POST request using cURL. Further, I am using HTTP POST method for sending or receiving data to remote server.

cURL POST Command Format

To illustrate, I am writing cURL post command format for making a POST request as below.

curl -X POST [options] [URL]

cURL POST Request

Of course, you can change the normal GET to a POST method by simply adding -X POST in a command. Specifically, I am writing the same example command below to give an idea on simple post command.

curl -X POST http://example.org/

POST Request without Data

For example, we need to browser some web location with using POST request than we can use below methods.

  1. Simple request –
    • curl --data '' https://example.com/resource.cgi
  2. Mention POST in the Request  –
    • curl -X POST https://example.com/resource.cgi
  3. Now, using POST request method  –
    • curl –request POST https://example.com/resource.cgi

Multipart POST

Generally, a POST request is sent via an HTML form. Therefore, The data send to the form is usually encoded in either multipart/form-data or application/x-www-form-urlencoded content type.

An HTTP multipart request is an HTTP request that HTTP clients construct to send files and data over to an HTTP Server. It is commonly used by browsers and HTTP clients to upload files to the server.

So, we need to make a post request using this command.

curl --form "fileupload=@MyfileName.txt" https://example.com/resource.cgi

Now, sometimes, it is required to send field names with the file name. I included the second command for this purpose.

curl --form "fileupload=@MyfileName.txt;filename=SecondFileName.txt" --form param1=value1 --form param2=value2 https://example.com/resource.cgi

Consequently, If we use -F option, curl sends the data using the multipart/form-data Content-Type.

Specifying the Content-Type

Finally, You can set a specific header or Content-Type using the -H option. The following command sets the POST request type to application/json and sends a JSON object.

curl -X POST -H "Content-Type: application/json" \
    -d '{"name": "linuxize", "email": "linuxize@example.com"}' \
    https://example/contact
Share

You may also like...