Sending POST Request with Curl

To make a POST request with Curl, you can run the Curl command-line tool with the -d or --data command-line option and pass the data as the second argument. Curl will automatically select the HTTP POST method and application/x-www-form-urlencoded content type if the method and content type are not explicitly specified. To post data in the multipart/form-data format, you can use the -F or --form command line option. To send data using another HTTP HTTP method, you use the -X command line option. If you would like to send data to the server via a different HTTP method, you can use the -X command-line option. Click Run to execute Curl POST Request Example online and see results.
Sending POST Request with Curl Run
curl -X POST https://reqbin.com/echo/post/json -d "key=value"
Updated: Viewed: 47802 times

What is Curl?

Curl is a popular command-line tool that allows you to send requests to the server, upload files, and submit web forms. Curl supports over 25+ protocols, including HTTP, HTTPS, SFTP, FTP, and has built-in support for web forms, SSL, user authentication, and HTTP cookies. Curl works on Linux, Windows, and Mac.

What is HTTP POST request method?

HTTP POST is one of the 9 common HTTP request methods supported by HTTP. The HTTP POST method allows clients to send and servers to receive and process the data contained in the body of a POST message. Sending data in the body of a POST message is optional; some POST requests may not have content in the body, such as requests that only want to update the status of an entity in the database. The POST method is often used for submitting login and contact forms to the server and uploading files and images. The HTTP POST method is used for CRUD operations to create or update a resource on the server. POST requests may change the state of the server and are not idempotent.

How to send a POST request using Curl?

You can send a POST request with Curl by explicitly specifying the POST method with the -X POST command line parameter or passing data to Curl using the -d or --data command line parameter. If you pass data using the -d command-line switch and do not explicitly specify the HTTP method with the -X command-line option, Curl will automatically select the HTTP POST method for the request.

Curl POST Request
curl -X POST https://reqbin.com/echo/post/json 
   -H "Content-Type: multipart/form-data"
   -d '[post data]'

Curl POST Request Syntax

The following is a Curl command to create a POST request with a message body:

Curl POST Request with Body
curl -X POST -H "[content type]" -d "[post data]" [options] [URL]

Where:
  • -X: the parameter specifies which HTTP request method will be used when communicating with the server
  • -H: the content-type header indicates the data type in the request body
  • -d: the parameter specifies the data to be sent to the server in the POST message body

The -X parameter specifies which HTTP request method will be used when communicating with the server. The -d parameter specifies the data to be sent to the server in the body of the POST message. The content-type header indicates the data type in the request body.

Curl POST Request Examples

The following are examples of sending a Curl POST request with a detailed description:

Posting a request body using Curl

To post data on the body of a request message using Curl, you must use the -d or --data command line parameter. The Content-Type header specifies the data type in the message body. The server will use this header to interpret and process the received data.

Curl POST Request Example
curl -X POST https://reqbin.com/echo/post/json 
   -H "Content-Type: application/json"
   -d '{"Id": 79, "status": 3}'  

Posting JSON data using Curl

If you want to post JSON data with Curl, you need to set the Content-Type to application/json and use the -d parameter to pass JSON to Curl. The command line parameter -H "Content-Type: application/json" specifies the JSON content type. JSON data should be passed as a string.

Curl POST JSON Example
curl -X POST https://reqbin.com/echo/post/json
   -H 'Content-Type: application/json'
   -d '{"e-mail":"my_mail","password":"my_password"}'

Posting form data using Curl

If you want to post form data with Curl, you can use the -d (--data) command-line parameters. With the -d command-line parameter, form data is sent as "application/x-www-form-urlencoded".

Curl POST Form Example
curl -X POST https://reqbin.com/echo/post/form
   -H "Content-Type: application/x-www-form-urlencoded" 
   -d "key1=value1&key2=value2" 

Posting a file using Curl

If you want to post a file with Curl, add the -d and -F command-line options and start the data with the @ symbol. Following the @ symbol, you must specify the file path. By default, Curl provides the Content-Type header based on the file extension, but you can provide a custom Content-Type header using the -H command-line option.

Curl Post File Example
curl -d @data.json https://reqbin.com/echo/post/json

Posting XML using Curl

To post XML data to the server using Curl, you can pass the XML with the -d command-line option and specify the data type in the body of the message with the -H "Content-Type: application/xml" option.

Curl Post XML Request Example
curl -X POST https://reqbin.com/echo/post/xml
   -H "Content-Type: application/xml"
   -d "<Request><Id>1</Id><User>Leo</User></Request>"

Sending basic auth credentials with Curl POST request

You can pass Basic Authentication credentials to Curl using the --user="login: password" command-line option. Curl automatically encrypts the user's credentials into a base64 encoded string and passes them to the server using the Authorization: Basic [token] header.

Curl Post Basic Authentication Request Example
curl -X POST https://reqbin.com/echo/post/json
   --user "login:password"

See also

Generate Code Snippets for Curl POST Example

Convert your Curl POST request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the ReqBin code generator.