a shot of dev knowledge

Related tags

How to perform a POST request using Curl

Curl is a command-line utility that allows users to create network requests. Curl is accessible on Windows, Linux, and Mac, making it the go-to choice for developers across all platforms.

We can make POST requests with varying levels of detail. Some sample POST requests are given below:

Making a simple POST request

To make a basic POST request using curl, type the following command on your command-line:

curl -X POST https://example.com/

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.

Sending additional fields with a POST request

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values.

curl -d "user=user1&pass=abcd" -X POST https://example.com/login

POSTing with curl’s -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded.

Specifying the Content-Type in POST request

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 -d '{json}' -H 'Content-Type: application/json' https://example.com/login

Sending a file using curl

We can also send complete files in the command-line using curl. The command used to do this is:

curl --form "fileupload=@my-file.txt" https://example.com/resource.cgi

Related tags

RELATED COURSES
View all Courses
Related Courses
Related Courses
View all Courses
Keep Exploring