cURL POST is used to send data to the server, while cURL GET request is used to get data from the server.
Key takeaways
cURL is a command-line tool used by developers to transfer data between clients and servers using various network protocols.
POST requests are used to send data to a server. They are commonly used for submitting forms’ data, APIs, and file uploads.
The basic cURL command looks like curl [options] -X POST [URL]
.
Some examples of how we can use cURL are as follows:
We can use the curl -X POST [URL]
command for simple post requests.
We can send data with the curl
command using the -d
option: curl -X POST [URL] -H "Content-Type: application/json" -d '{"key": "value"}'
We can upload files using -F
flag, curl -X POST -F "file=@filepath.jpg"
.
Daniel Stenberg introduced cURL (curl), a command-line tool that enables users to make network requests. The word cURL stands for “Client for URL.” Developers choose cURL because it is available on the main operating systems, including Windows, Linux, and Mac. If not pre-installed, we can use operating system’s package manager to install it easily.
Utilizing cURL for HTTP POST requests—which are used to send data to a server—is one of its most popular applications. This can be useful for tasks such as submitting form data, interacting with APIs, or uploading files.
curl
command for the POST
requestTo perform a simple POST
request, we can use the following command:
curl [options] -X POST [URL]
[options]
: This option reprents various flags and parameters to customize the request.-X POST
: This specifies that the HTTP method should be POST
.[URL]
: This is the URL to which the POST
request is sent.Here are some of the commonly used options with curl
for making POST
requests:
Flags | Aliases | Description | Example |
|
| Adds a header to the request |
|
|
| Adds data to be sent in the request body |
|
|
| Sends data as form field |
|
|
| Adds basic authentication credentials |
|
|
| Shows detailed information about the request and response |
|
POST
requestWe can make a basic POST
request using cURL by using the command below:
curl -X POST https://educative.com/api/
The -X
flag specifies a custom request method to use when communicating with the HTTP server. By default, the GET
method is used unless some other method is specified.
Users can send data with the POST
request using the -d
flag. It post the request body using the curl
command in the following way:
curl -X POST https://educative.com/api/login/\
-H "Content-Type: application/json" \
-d '{"name":"Arya", "age":16}'
POST
requestThe following POST
request sends a user
and a pass
fields along with their corresponding values.
curl -d "user=user1&pass=abcd" -X POST https://educative.com/api/login
Posting with curl’s -d
option will include a default header that looks like: Content-Type: value
.
Content-Type
in POST
requestA POST request’s Content-Type
header inform the server of the type of data being sent. We can use the -H
(or --header
) option in curl
command to specify the Content-Type
header.
The -H
flag can be used to send a specific data type or header with cURL. The following command sends a JSON object with the request.
curl -H 'Content-Type: application/json' \
-d '{"name":"Arya", "age":16}'
https://educative.com/api/login
multipart/form-data
in POST
requestmultipart/form-data
is often used for uploading files along with other form data. Use the -F
(or --form
) option to specify form fields and files.
curl -H "Content-Type: multipart/form-data" \
-F name=arya \
-F season=2 \
-F Rating="5" \
-X POST \
https://educative.com/api/data
POST
requestThe -F
flag is used to specify form fields (including images or files). In the code snippet below, the @
symbol is used to indicate that the data should be read from a file when uploading an image, rather than including the data directly in the request body.
curl -X POST https://educative.com/api/upload \
-F "image=@/path/to/image.jpg"
Users can send XML data by specifying the Content-Type
header to application/xml
or text/xml
and use the -d
(or --data
) option to include the XML content.
curl -X POST https://educative.com/api/data/xml
-H "Content-Type: application/xml"
-d " <author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>"
Form data is frequently used in web-based applications and is usually encoded with a URL. Set the Content-Type
to application/x-www-form-urlencoded
and use the -d
option for URL-encoded data.
curl -X POST https://educative.com/api/submit-form \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=arya&password=got"
We can also send complete files in the command-line using cURL. We can use the command given below:
curl -X POST --form "file=@/pathtoimg.jpg" https://educative.com/api/post
Here, we use --form
(or -F
) option, and the file=@/pathtoimg.jpg
part inform cURL to include the contents of img.png
as part of the form data, with the field name file
.
POST
requestTo send basic auth credentials with cURL POST
request includes the --user username:password
option in the curl
command. cURL automatically encodes these credentials into a Base64 string and includes them in the request header as Authorization: Basic [token]
.
curl -X POST https://educative.com/api/login \
--user "username:password"
In the following example, we'll use a cURL POST
request to interact with API. We use the POST
request using the curl
command. We insert a new item using the POST
request and receive an object with a new ID.
Quiz!
Which option is used to include a header in a curl
request?
-F
-d
-H
-X
Haven’t found what you were looking for? Contact Us
Free Resources