Introduction
Before we begin talking about how to make a post request with cURL, let's briefly understand - What is cURL?
cURL is a command-line tool for sending data from or to a remote server using one of the protocols supported. It comes pre-installed on Mac OS X and most Linux variants.
Developers use cURL to test APIs, inspect response headers, and make HTTP queries. To transfer data to a remote server, utilize the HTTP POST method.
We'll show you how to make POST requests with cURL in this tutorial. We will also address a few FAQs on how to make a POST request with cURL.
Create a POST Request
To make a POST request, use the curl
command in the following format:
curl -X POST [options] [URL]
-X
option specifies which HTTP request method will be used.The Content-Nature header of the request body specifies the type of the request body.
A POST request is often sent through an HTML form. The data sent to the form is commonly encoded in one of two content types: multipart/form-data
or application/x-www-form-urlencoded
.
Use the -F
option, then the field=value
pair to make a POST request. The example below shows how to create a POST request to a form with fields for "name" and "email":
curl -X POST -F 'name=vegastack' -F '[email protected]' https://example.com/contact.php
Curl delivers data using the multipart/form-data
Content-Type when the -F
option is specified.
The -d
option is another way to make a POST request. This tells curl
to use the application/x-www-form-urlencoded
Content-Type to convey the data.
curl -X POST -d 'name=vegastack&[email protected]' https://example.com/contact.php
Specifying the Content-Type
Use the -H
option to specify a specified header or Content-Type. The following command transmits a JSON object and sets the POST request type to application/json
:
curl -X POST -H "Content-Type: application/json" \
-d '{"name": "vegastack", "email": "[email protected]"}' \
https://example/contact
Uploading Files
Simply add the @
symbol before the file path to POST a file with curl. An archive, image, document, or other types of file can be used.
curl -X POST -F 'image=@/home/user/Pictures/wallpaper.jpg' http://example.com/upload
FAQs to Make a POST request with cURL
How can I specify POST data with cURL?
To include POST data, use the -d
or --data
flag followed by the data you want to send. For example, -d "param1=value1¶m2=value2"
.
Can I include JSON data in a POST request with cURL?
Yes, you can include JSON data by using the -H
or --header
flag to set the Content-Type
header to application/json
, and then passing the JSON payload using the -d
flag.
How do I set custom headers in a POST request with cURL?
Use the -H
or --header
flag, followed by the header name and its value. For example, -H "Authorization: Bearer token"
.
Can I upload a file in a POST request with cURL?
Yes, you can upload a file by using the --data-binary @file.txt
flag, replacing "file.txt"
with the path to your file.
How do I handle cookies in a POST request with cURL?
To handle cookies, you can use the --cookie
flag followed by the data from a cookie file, or use the --cookie-jar
flag to save cookies to a file.
How can I make an authenticated POST request with cURL?
You can include authentication credentials using the -u
or --user
flag followed by the username and password in the format username:password
.
Can I follow redirects in a POST request with cURL?
Yes, you can enable redirect following using the -L
or --location
flag. This allows cURL to automatically follow redirect responses.
Conclusion
We've already taught you how to make POST requests with curl
. Visit the documentation page for more information.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.