cURL is one of the most useful command line utilities known to Linux users.
This tool is great for testing API functionalities, downloading files, checking response headers and making HTTP requests. Though many developers are leaning towards other programs such as postman to test APIs, but cURL still a strong option in this domain.
One common HTTP request used by developers is the HTTP POST request using cURL.
In this article you will learn how to make HTTP POST requests using cURL, covering the common methods to handle different data types and situations.
We will keep things fairly simple and cover only the POST part of the curl command tool.
Table of Contents
Perform a simple POST request
The syntax for making a POST request with curl
is:
curl -X POST [option] [URL]
One of the easiest HTTP requests that we can send with the help of curl
is the POST request without specifying a body to it. By default, the curl
tool sends GET requests; however for our objective we will use the --X
command line argument to specify our request method; this option is used to communicate with the remote server, which is POST in this example. The command mentioned below will send the request and print out the response body.
curl -X POST localhost:8080/edxd
Perform a POST request with data
Whenever dealing with POST requests, there is a high chance that you will be adding some data along with your request. We can specify these data/ parameters with the -d
, --data
command line option. This option will make the curl
request to set the Content-Type header with its default value: “application/x-www-form-urlencoded”.
curl -X POST --data "user=edxd&password=linux" localhost:8080/edxd/login
Note: – This command will cause curl
to behave the same way as it does when you a fill a login form and submit the data.
-Whenever you add the -d, --data
option without adding the --X
option, curl
will send a POST request by default.
We can also add multiple -d, --data
option for each parameter. Check the example below.
curl -X POST --data "user=edxd" --data "password=linux" localhost:8080/edxd/login
Perform a POST request with the Content Type header
To identify the content type of our POST request to the server, we have to add the --H
command line option to supply our request with the Content- Type header and its value. Let’s try to add some new data to our web application using different types of data.
Send JSON Data
Check the invalid command below that sends a JSON object along with the request.
curl -X POST --data '{"Firstname": "John", "Lastname": "Doe", "Email": "[email protected]", "Password": "jdow"}' localhost:8080/edxd/signup
Note: We will receive an error that informs us that the endpoint only accepts JSON data. So it’s imperative that we explicitly specify the content type header, otherwise the curl tool will use the default value.
curl -X POST --data '{"Firstname": "John", "Lastname": "Doe", "Email": "[email protected]", "Password": "jdow"}' --H "Content-Type: application/json" localhost:8080/edxd/signup
Send XML Data
The next command will send the same data as the previous JSON example, however in a different data form. To inform the web application that we are sending an XML object, simply change the content type value to “text/xml”.
$ curl -X POST --data '<newuser> <firstname> John </firstname> <lastname> Doe </lastname> <email> [email protected] </email> <password> jdow </password> </newuser>' --H "Content-Type: text/xml" localhost:8080/edxd/signup
Send Pain text Data
Suppose that our web application has a functionality to handle POST requests with plain text string. For this case, we will simply change the previous curl
request --d
, --data
and -H options accordingly. Check the curl
command mentioned below.
curl -X POST --data 'John Doe [email protected] jdow' --H "Content-Type: text/plain" localhost:8080/edxd/signup
Perform a POST request to upload a file
Using the curl command, we can easily transfer entire files by adding the --F
, --form
command line option, plus specifying the desired file path. Check the mentioned curl
command bellow.
curl -X POST --F file=@"home/edxd/myfile.dat" localhost:8080/edxd/bucket
Note: The -F option will make the curl
request to set the Content-Type header with its default value: multipart/form-data
.
We can also add multiple files simultaneously within the same request. This can be achieved by separating the file paths with a semicolon ;
.
curl -X POST –F file=@"home/ubuzz/myfile.dat;file=@"home/edxd/mySecondfile.dat" localhost:8080/edxd/bucket
The curl
tool offers the possibility to specify a different name for our file other than its original, when the web application receives it.
curl -X POST –F file=@"home/edxd/myfile.dat;filename=changedName.dat" localhost:8080/edxd/bucket
Note: in this example, the web application cannot see the original file name (myfile.dat).
With curl we can as well define the MIME type of our file just as so:
curl -X POST –F file=@"home/edxd/myfile.pdf;type=application/pdf" localhost:8080/edxd/bucket
[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Note that you can choose other types of data, such as image types: (image/jpeg, image/png, and image/svg+xml), video types, and more.
Moreover, when the curl
command has an unspecified type, then by default it sets the type to application/octet-stream.
[/powerkit_alert]
Perform a POST multipart request
One of the neat tricks of curl
, is that it provides a choice to send a POST request to a form simulating multi data form submission. The curl
POST request mentioned below is constructed to send files and data to our web application’s endpoint using the --F --form
command line option.
curl -X POST –F file=@"home/edxd/myfile.pdf;type=application/pdf" --F user="edxd" --F password="linux" localhost:8080/edxd/addform
Note that the user and password are actually text/ password fields within a form. The syntax for this command line option is: Field=”Value”
For this example, curl
sets by default the Content type to “multipart/form-data”
Conclusion
In this how to article we’ve walked you though the different ways to make POST requests with curl
. We hope they may come in handy for you.