Curl POST: Complete Guide with Examples

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 for the transmitted data. The Content-Type header indicates the data type in the request message's body. The server requires this header in order to interpret and process the data in the body of the POST message correctly. If you need to send a different content type with your Curl POST request, you can pass the content type with the -H command-line option. If you would like to send data to the server via another HTTP method, you can use the -X command-line option. Click Run to execute Curl POST Request Example online and see results.
Curl POST: Complete Guide with Examples Run
curl -X POST https://reqbin.com/echo/post/json
   --user "login:password"
Updated: Viewed: 6074 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?

The POST method is one of the nine standard methods of the HTTP protocol. The POST method is one of the nine standard methods of the HTTP protocol. 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.

Curl POST Request Syntax
curl -X POST [options] [URL]

How to post request body with 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}'  

How to post 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 the Curl. The command line parameter -H "Content-Type: application/json" sets the JSON content type. JSON data is 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"}'

How to post form data using Curl?

If you want to post form data with Curl, you can use the -F (--form) or -d (--data) command-line parameters. With the -F command-line parameter, form data is sent as "multipart/form-data", and 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" 

How to post 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

How to post 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>"

How to send 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, Curl/Bash, Python, Java, C#/.NET code snippets using the ReqBin code generator.