Trusted answers to developer questions

How to perform a POST request using Curl

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

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

Code example

We used the Fake Store APIIt is open source API that can be used for Pseudo-real data for testing our API calls. to demonstrate the POST request using the curl command. We inserted a new item using the POST request and received an object with a new ID.

Terminal 1
Terminal
Loading...

RELATED TAGS

curl
post
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?