How To Make POST Request with cURL?

The cURL is computer software and command-line tools used to make requests for different protocols. But the most popular usage with the curl command is making HTTP post requests. Even the curl command-line tool is created for the Linux operating systems it is cross-platform which can be used for Windows, MacOSX, BSD, etc. In this tutorial, we will learn how to make different HTTP POST requests by using curl.

Make simple POST Request

We start with a simple example where we make a POST request to the specified URL. The -X option is used to specify the request type which is POST and we also provide the URL.

curl -X POST http://www.linuxtect.com/

If the -X POST is not specified the GET method of the HTTP protocol is used by default.

Send Additional Fields with POST Request

The POST request can be used to send some data to the remote URL with the POST request. The data can be specified via the command line by using the -d option and data like below.

curl -d "name=ismail&password=123" -X POST http://www.linuxtect.com/

The provided data is separated with the & sign and generally structured as name=value. Using the -d option also adds some implicit HTTP headers like Content-Type etc.

Specify HTTP Request Header

The curl command is very flexible where HTTP headers can be specified via the command-line interface. The -H option is used to specify the HTTP header value. The b option can be used multiple times to specify multiple HTTP headers.

curl -H "Transfer-Encoding: chunked" -X POST http://www.linuxtect.com/

Specify Content Type with POST Request

By using the -H option the content type of the request can be easily specified. b is a popular standard and content type which can be used to communicate with the server. In the following example, we set the content type as JSON.

curl -H "Content-Type: application/json" -X POST http://www.linuxtect.com/

Specify Cookie with POST Request

Some web applications use authentication and authorization which rely on cookies. The curl command can be used with a cookie to access restricted resources by authenticating requests. The -b or –cookie option can be used to specify the cookie data.

curl --cookie "4rwedrw34dfawefd" -X POST http://www.linuxtect.com/

Upload/Send File

Another useful feature of the curl command is the ability to upload local files to the remote server. The local file can be specified with the –form option. In the following example, we use curl to upload a file named “file.txt“.

curl --form "[email protected]" -X POST http://www.linuxtect.com/

Send JSON Data

One of the most popular usecase for the curl command is using the JSON format for cummunication especially for request. In the following example we send a JSON data to the server.

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST "http://localhost/data"

Alternatively the JSON data can be located in a file and this file JSON data can be send to the server like below.

curl -d "@mydata.json" -X POST "http://localhost/data"
READ  Grep Search Case Insensitive String - Ignore Case

Leave a Comment