Introduction
curl
is a command line utility for transferring data to or from a remote server using one of the supported protocols. It is installed by default on Windows, macOS, and most Linux distributions.
System administrators, developers, and other users use curl
to test APIs, view response headers, and make HTTP requests.
This article explains how to use curl to make POST requests.
Curl options
The HTTP POST method is used to send data to the remote server.
The general form of the curl
command for making a POST request is as follows:
curl -X POST [options] [URL]
The -X
option specifies which HTTP request method will be used when communicating with the remote server. In most cases, you do not need to set the method because it is determined by command-line options.
Make a POST request
A typical POST request is sent via an HTML form, and the data sent to the form is usually encoded in the application/x-www-form-urlencoded
format type. The keys and values of the data are encoded in key-value pairs, separated by the &
symbol and separated =
between the key and the value.
To send a POST request with curl
, use the -d
( --data
) option to specify the data:
curl -d 'name=noviello&[email protected]' https://example.com/form/
In the example above, we are sending data to the remote server consisting of two key-value pairs: “name=noviello” and “ [email protected] ”.
You can also send data using multiple -d
options. For example, the command above can also be written like this:
curl -d 'name=noviello' -d '[email protected]' https://example.com/form/
When sending data to curl
using the -d
option, you must always correctly URL encode all non-alphanumeric characters in both keys and values. For example, if you are sending data that contains a name with spaces ("John Doe"), the command will look like this:
curl -d 'name=John%20Doe' https://example.com/form/
However, it is a bit inconvenient to manually encode data that is not already encoded. In this case it is better to use the --data-urlencode
option which tells curl
to encode the supplied data. The above command, when used with the --data-urlencode
option, will look like this:
curl --data-urlencode 'name=John Doe' https://example.com/form/
Data passed to the curl
command is usually in the form key=value
. When you use the --data-urlencode
option, only the value part is encoded. curl
expect the key to already be URL encoded.
If you are sending data that contains a large number of key-value pairs, instead of typing the data on the command line you can store it in a file and pass that file to curl
:
curl -d @name_of_the_file https://example.com/form/
When the data supplied to the command begins with the @
symbol, curl
treats the data as a file on the local system and will read and use the data from that file.
Make a POST request using the multipart form data
The multipart/form-data
content type is used when the form data contains binary files or other large payload data.
To create a multipart POST request, invoke the curl
command with one or more -F
( --form
) options, followed by key=value
pairs. When the -F
option is used, curl
sends data using the multipart/form-data
content type.
The following example shows how to make a POST request to a form that has "name" and "image" fields:
curl -F 'name=noviello' -F '[email protected]' https://example.com/form/
File upload
To POST a file with curl
, add the @
symbol before the file path. The file can be an archive, an image, a document, etc.
curl -F 'image=@/home/user/Pictures/wallpaper.jpg' http://example.com/upload/
POST JSON data with cURL
JSON is a text-based data format used for data transfers between web services and APIs.
You can send JSON data using POST using curl
the --json
option.
Here's a basic example of sending a JSON object to a server:
curl --json '{"website": "noviello.it"}' http://example.com/api/
curl
sends JSON data as-is, so make sure it's in valid JSON format.
As with normal POST requests, you can use multiple --json
options in a single command:
curl --json '{"name": "Jonn"}' --json '{"age": "36"}' http://example.com/api/
JSON data can also be read from a local file:
curl --json @json_data.txt http://example.com/api/
If you're using curl
a script, you can pipe the JSON data from another command to curl
, as shown below:
echo '{"website": "noviello.it"}' | curl --json @- http://example.com/api/
The @-
part means reading the body from standard input.
Conclusion
We showed you how to use curl
to make POST requests. For more information about curl
, visit the Curl documentation page.
If you have any questions or feedback, feel free to leave a comment.