How to make a POST request with cURL - TechvBlogs

How to make a POST request with cURL

In this article, we’re going to explain how to use cURL to make POST requests. The HTTP POST method is used to send data to the remote server.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 year ago

TechvBlogs - Google News

What is cURL?

Curl is a command-line utility that allows users to create network requests. Curl is accessible on Windows, Linux, and Mac, making it the go-to choice for developers across all platforms.

A cURL is computer software and command-line tool 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 and can be used for Windows, MacOSX, BSD, etc. In this tutorial, we will learn how to make different HTTP POST requests by using curl.

In this article, we’re going to explain how to use cURL to make POST requests. The HTTP POST method is used to send data to the remote server.

Making a POST request

The general form of the curl command for making a POST request is as follows:

curl -X POST [options] [URL]

The -X the option specifies which HTTP request method will be used when communicating with the remote server.

The type of the request body is indicated by its Content-Type header.

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

Make a 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://example.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 "firstname=John&lastname=Andrew" -X POST http://example.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.

Specifying the Content-Type in the POST request

The -H a flag can be used to send a specific data type or header with a curl. The following command sends a JSON object with the request.

curl -d '{json}' -H 'Content-Type: application/json' https://example.com

Read Also: How to install and Secure Redis on Ubuntu 22.04

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 "sadad1321saweqe" -X POST http://example.com

Sending a file using curl

To POST a file with curl, simply add the @ symbol before the file location. The file can be an archive, image, document, etc.

curl -X POST -F 'image=@/home/user/Downloads/profile.jpg' http://example.com/upload

Send JSON Data using curl

One of the most popular use-cases for the curl command is using the JSON format for communication, especially for requests. In the following example, we send JSON data to the server.

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

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

curl -d "@mydata.json" -X POST "http://example.com/data"

Conclusion

This article shows the importance of the Curl command in Linux and discusses the usage of curl post requests on Linux. I clarified how to make POST requests using the CURL command.

Thank you for reading this article.

Read Also: How to create custom helper functions in Laravel

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Comments (0)

Comment


Note: All Input Fields are required.