How to Specify HTTP POST Method in cURL

Using curl to send HTTP POST requests is a fundamental skill for testing APIs and interacting with web services. This guide provides a direct, step-by-step overview of how to specify the POST method using curl, demonstrating how to send empty POST requests, submit form data, and transmit JSON payloads.

Using the -X Option

The most explicit way to specify the HTTP POST method in curl is by using the -X (or --request) option followed by POST. This tells curl to use the POST method instead of the default GET method.

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

Using the -d Option (Implicit POST)

When you send data using the -d (or --data) option, curl automatically assumes you want to make an HTTP POST request. You do not need to explicitly include -X POST when using this option.

To send standard form data (application/x-www-form-urlencoded):

curl -d "param1=value1&param2=value2" https://api.example.com/endpoint

Sending JSON Data with POST

To send a JSON payload, you must combine the -d option with a custom header specifying the Content-Type as application/json. While the -d flag implicitly sets the method to POST, explicitly adding -X POST is a common practice for clarity.

curl -X POST https://api.example.com/endpoint \
     -H "Content-Type: application/json" \
     -d '{"name": "John Doe", "email": "john@example.com"}'

Sending Multipart/Form-Data (File Uploads)

If you need to upload files or submit multipart form data, use the -F (or --form) option. Like the -d option, using -F automatically changes the request method to POST.

curl -F "profile_picture=@/path/to/image.png" -F "username=johndoe" https://api.example.com/upload