Are you new to the curl technology and want to learn how to send a POST request using this versatile command line utility tool? Then, you are on the right page, as the article below will show you how to get that done.
The curl technology is one of the technologies that looks easy yet powerful and is so versatile in its usage that you can be using it without realizing it. In fact, it is said that every human being who uses the Internet makes use of curl with or without realizing it. From your smartphones, television sets, medical devices, cars, and even printers all use curl as their Internet transfer engine. This makes it quite popular, with over 20 billion installations.
However, while its actual usage in applications is so integrated into our daily lives, it is done at a lower level that even those you expect to know how to use it, such as developers, do not. In this guide, I will be discussing how to make use of the curl technology to send POST requests. You will also learn how to send JSON data and files using the curl tool. Before that, let’s take a look at an overview of curl.
cURL — an Overview
cURL is a command line tool and library for transferring data using URLs. This tool has been in existence since 1998, and its usage is still on the increase even though there are competing products out there. cURL is used from the command line, and libraries have also been created for many of the popular languages so that you can use them in your scripts. It is basically a data transfer tool that makes use of the Internet.
This command line utility tool supports over 25 protocols, including HTTP, HTTPS, SOCKS5, FILE, FTP, SMTP, SMTPS, and many others. Below is the “Hello World” of curl python.
The above code will send a get request to https://www.example.com and return the content of the page as a response.
Guide on How to Send cURL Post Requests
The curl tool comes installed on the later versions of Windows, starting from Windows 10, macOS starting from Jaguar, and even the later versions of Linux distributions. So, there is a change curl is already installed on your computer. If you want to verify whether it is installed or not, enter “curl” and press enter in the command line. You will see the below printed in the command line or terminal, depending on whether you are using a Windows or macOS, respectively.
curl: try 'curl --help' or 'curl --manual' for more information
If you see the above, this means curl is installed. If not, you will see an error message instead. If it is not installed, it means you need to install it. Go to the curl download page to install the curl tool on your computer.
Sending Simple POST Request
To send a simple POST request that does not send any data along, you can use the below command. The website used is an example; you can use your own to try it out.
curl -X POST https://example.com/
Let me explain the above for you to understand what we did here.
First, the curl keyword is self-explanatory, telling the command line that the command is a curl command.
—X flag is for specifying the request method. If you check the first example, we didn’t include it, and the request went through. This is because without specifying, the default request method, which is GET, will be used.
POST is used to tell the curl tool that the request to send is the POST request. If it is a PUT request, the value is going to be PUT and likewise DELETE.
https://example.com/ — this is the URL the POST request will be sent to.
The above is the simplest form of POST request. And for the most part, this is just for illustration. This is because, in most cases, you will be sending some data alongside your request either as a JSON object or even a file. I will show you how to get both done below
How to send POST Request with Data
If you want to send data alongside the POST request, the syntax changed a little as you need to introduce the header tag and then the data tag. Below is the syntax to do that.
curl -X POST -H "[content type]" -d "[post data]" [options] [URL]
Now, let's explain what the above keywords mean before giving an example of how to use them.
You already know what the — X flag is meant for, you know what curl and POST mean, too.
—H is the header tag for specifying the headers. It is used for “[content type]” and other response header specifications.
—d is for specifying the JSON data to be sent alongside the request.
Now, let's take a look at an example of how to send a POST request with data included.
curl -d "user=user1&pass=abcd" -X POST -H 'Content-Type: application/json’ https://example.com/login
As you can see above, we sent username (user) and password (pass) with the values user1 and password abcd. The content type is application/json and is specified after the —H flag. The above will send the POST request with the data included.
How to Send Files via cURL
You cannot only send post data via cURL, but it also does support the sending of files. Which is surprisingly a part of a good number of applications. The method of getting this done is easy and straightforward, especially if you have the file in the current path. Below is the cURL command format for sending files.
curl –form “fileupload=[“filename”]” [“target URL”]
As you can see from the above, you can use the — form flag to tell cURL you want to send a file via cURL in the command line. The filename in the command is a placeholder for the name of the file and extension, while the target URL is the URL you want to send the file to. Below is an example of how to construct this command.
curl --form "[email protected]" https://example.com/resource.cgi
With the above command, the file will be sent to the target URL.
Q. How Do I Send a POST cURL Request in Postman?
Postman is one of the tools that support cURL. If you have the cURL command, you can configure your request using it on Postman. How to do this is simple. Copy the cURL command and go to the Postman software or web app, depending on the version you use. From the sidebar, select import and paste the cURL command in the space provided and click import. This will create a new request for you. Inspect the request body and header and tweak it the way you want.
Q. Can I Copy the cURL Command from a Web Browser?
If you are a developer looking to replicate requests being sent by your browser, you can do that using a combination of your browser’s developer tool, cURL, and Postman. First, you should go to your browser developer tool and go to the network tab. Choose only the fetch/xhr request — for this guide purpose. You will see the requests sent behind the scenes by your browser. Choose one of the requests and right-click on it. You will see an option for copy, and there, there are multiple copy formats — choose copy as cURL. Take the copied cURL command and import it to play around with it.
Q. How to Use cURL in Python?
Python remains one of the most popular programming languages currently being used. If you are a Python programmer, there are two options available to you if you want to make use of the cURL tool. The first method is by using the subprocess module, which allows you to run external codes in Python. The second method, which many would find easier, is by using the pycURL library, which is a library for interacting with the cURL tool.
Looking at the above, you can see how to send web requests using the cURL tool. The guide above has shown how to send simple get requests with just the curl keyword and the URL. It then went ahead to show to send requests with the request methods explicitly stated. Also discussed is how to send post requests with data either as JSON or as a file. With the article above, you should know how to send web requests using the cURL tool.