How to Perform a POST Request Using Curl

Curl command is referred to as a “Client URL” and universally used command line utility available for Windows, Mac, and Linux operating systems. This command uses different protocols such as HTTPS, FTP, SMP, and so on to download images and files from the URL, send data to the server, access data from the URL, and get the server content. Sending data to the server is done through Post request while getting data from the server is done through Get request.

This article will demonstrate how to perform a POST request using the curl command.

How to Perform a POST Request Using curl Command?

In a POST request, the “POST” is an HTTP request method utilized to send requests or data to the server through HTTPS or HTTP protocol. The “curl” command helps us to send a POST request to the server through the URL.

Prerequisites: Install Curl Command

To send a POST request to a server through the Client URL, first, install the “curl” command on Ubuntu using the “apt install curl” with “sudo” rights:

sudo apt install curl

 

Send Simple Post Request Using Curl

In order to send simple POST requests through the curl command, utilize the “curl -X POST <URL>” command:

curl -X POST https://reqbin.com/

 
Here, “-X” is used to select the HTTP request method such as the “POST” method.


 

Send Data in Post Request Using Curl

The POST request method is particularly used to send or transfer data to a server or specified URL. To send data to the server, utilize the following options:

    • -d” option specifies the data that needs to be transferred.
    • -H” defines the header content type or data type that is transferred through a POST request.
    • -X” is utilized to choose and use the POST request method with URL:

 

curl -d '{"e-mail":"rafia@gmail.com","password":"password123"}' -H "Content-Type: multipart/form-data" -X POST https://reqbin.com/echo/post/json

 

 

Write Response of Post Request in Output File

The output or response of a POST request can be saved or overwritten to a file using the “-o” option. For instance, we have saved the response in the “test.html” file:

curl -d '{"e-mail":"rafia@gmail.com", "password":"password123"}' -H "Content-Type: multipart/form-data" -X POST https://reqbin.com/echo/post/json -o test.html

 

After that, execute the file and check the output or response of POST request:

Send File in Post Request Using Curl

POST requests not only send data but also can exchange files as well. In order to send the entire file in POST request, first, create the file using the “touch” command and save the content into the file.

For instance, we have created “file.txt”:

touch file.txt

 
Next, utilize the “curl” command along with the POST request as mentioned in the below command:

curl --form "fileupload=@file.txt" -X POST https://reqbin.com/echo/post

 
Here, “–form” option is used to send the form data in the POST request:


 

That is all about sending a POST request using the “curl” command.

Conclusion

To perform a POST request using the “curl” command, first, install the “curl” on your system through the “sudo apt install curl” command. After that, send the POST request in the “curl” command using the “curl -X POST <URL>” command along with options such as “-d”, “-H”, and “–form”. This post has illustrated how to perform a POST request using the curl command.



from https://ift.tt/AU4OjbV

Post a Comment

0 Comments