How To Make A POST Request With CURL.
In this article, we’re going to explain how to use cURL to make POST requests. The HTTP POST method is used to send data to the remote server.
cURL is a command-line utility for transferring data from or to a remote server using one of the supported protocols. It is installed by default on macOS and most Linux distributions.
Making a POST request
The general form of the curl command for making a POST request is as follows:
The type of the request body is indicated by its Content-Type header.
Generally, a POST request is sent via an HTML form. The data send to the form is usually encoded in either multipart/form-data or application/x-www-form-urlencoded content type.
To create a POST request, use the -F option, followed by the field=value pair. The following example shows how to make a POST request to a form that has “name” and “email” fields:
When the -F option is used, curl sends the data using the multipart/form-data Content-Type.
Another way to make a POST request is to use the -d option. This causes curl to send the data using the application/x-www-form-urlencoded Content-Type.
If the -d option is used more than once you can merge the data using the & symbol:
Specifying the Content-Type
To set a specific header or Content-Type use the -H option. The following command sets the POST request type to application/json and sends a JSON object:
To POST a file with curl, simply add the @ symbol before the file location. The file can be an archive, image, document, etc.
We’ve shown you how to use curl to make POST requests. For more information about curl, visit the Curl Documentation page.