Perform a POST Request Using Curl on Linux

How to Perform a POST Request Using Curl on Linux

If you are looking for a definitive guide on how to perform a POST request using CURL on Linux OS, this is just the guide for you. 

CURL is a command-line utility that can be used to make network requests to servers. It is available for the most popular operating systems like Linux, Windows, and Mac. Hence, it is the default choice for developers to test their APIs. 

This article will discuss how we can make a POST request using CURL on Linux. POST is an HTTP method used to send data to the server. Similarly, GET requests can be used to obtain the data from the server. Besides this, there are several other supported methods. The following figure illustrates some HTTP methods:

HTTP Methods and meaning

In the rest of the article, we will first briefly discuss CURL. Then, we will discuss how we can perform a POST request using CURL and its various options.

Introduction to CURL

cURL stands for client URL. A command line utility can make Hyper Text Transfer Protocol (HTTP) requests to your server. You can also transfer data from or to a remote server. The following are the features of CURL:

  • It can be used for protocols such as HTTP, FTP, SMP, etc.
  • You can use it to download images and files from servers.
  • You can also send data to the server and access data from the URL.
  • Developers can use it to test their APIs. 
  • Developers can use it for viewing response headers.
  • As a developer, you can make various HTTP requests using CURL.

Installation of CURL

Before we perform a POST request using CURL, you need to install it on your system. The installation is straightforward. For Ubuntu, you can install it with the help of the following command:

$sudo apt install curl
Installation of CURL

General usage of CURL

The general usage of the CURL command is as follows:

$curl -X POST [options] [URL]

There are many options you can use with the CURL command. Following are a few options:

  • -b, –cookie <name=data> : You can specify a cookie using this command.
  • -c, –cookie-jar <file name>: The response cookies are saved in the specified file.
  • -d, –data <data>: Sends the data in specified format.
  • -#, –progress bar: CURL will display a simple progress bar.
  • -F, –form <name=content>: You can submit form data.
  • -H, –header <header>: You can specify any header to be passed with the HTTP request.
  • -L, –location: This option can be used to enable redirection.
  • -o, –output <file>: Saves the output to a file.
  • -X, –request: The HTTP method to use. By default, GET methods are used.

Perform a POST Request Using CURL on Linux

Now, we will discuss how we can perform a POST request using CURL.

Making a simple POST request

To make a simple POST request, you can use the following command:

$curl -X POST

Here, -X is used to specify the HTTP method we used to make the request. 

Note: By default i.e., if you don’t specify an HTTP method, the GET method is used to request the server.

Sending additional data with the POST request

A POST request is normally sent in HTML form. The data is encoded as multipart/form-data or application/x-www-form-urlencoded content type. You can send additional data with a POST request. In that case, you will use the –F flag to specify the fields you want to send. The following example illustrates it:

$curl -X POST -F 'name=abc’  -F 'password=abc '

In the above scenario, CURL will send the data as multipart/form-data Content-Type.

You can also use fields such as pass user Id and password along with the POST request as follows:

$curl -d "user=user1&pass=abcd" -X POST

Here, we have used the –d option. In this situation, CURL will send the data as application/x-www-form-urlencoded Content-Type. You can also merge more that none field using & as follows:

$curl -X POST -d 'name=abc &email=abc'

Specifying content-type

The –h flag can specify a particular data type or header with the POST request. For instance, we can send a JavaScript Object Notation (JSON) object with POST as follows:

$curl -d '{json}' -H 'Content-Type: application/json' /login

Sending a file using CURL

In addition to sending headers, you can send a file to the server using CURL. First, create a file using the touch command as follows:

$touch my-file.txt

Now you can send the file along with the POST request. The following command illustrates it:

$curl --form "f[email protected]" /resource.cgi

In the above line, the –form is used to send the form data in the POST request.

Saving the response in a text file

The response of an HTTP POST request can be saved in a text file using the –o option. The following command saves the response in the test.html file:

$curl -d '{"e-mail":"abc", "password":"password123"}' -H "Content-Type: multipart/form-data" -X POST -o test.html

HTTP Response Codes for POST Requests

When making a POST request using CURL, the server will send a response back to the client. This response can include an HTTP status code, which indicates whether the request was successful or not.

Here are some of the most common HTTP response codes that you might encounter when making a POST request using CURL:

  • 200 OK: The request was successful, and the server has returned the requested data.
  • 201 Created: The server has successfully created a new resource based on the POST request.
  • 400 Bad Request: The request was malformed or invalid, and the server cannot process it.
  • 401 Unauthorized: The request requires authentication, and the user has not provided valid credentials.
  • 403 Forbidden: The server has received the request but refuses to authorize it.
  • 404 Not Found: The requested resource cannot be found on the server.

It’s essential to understand these response codes and their meanings. For instance, a 200 OK response code indicates that the server successfully processed the request and returned the requested data. A 404 Not Found response code, on the other hand, indicates that the requested resource cannot be found on the server.

In this article, we have discussed how we can perform a POST request using CURL on the server. We briefly introduced CURL and its installation in Linux. Then, we examined its various options. Finally, we discuss ways to make POST requests, such as passing form and JSON data, sending a file to the server, etc. We hope you learned everything you needed to from this article.

If this guide helped you, please share it. 

Related Posts