How do I post request body with Curl?

To post data in the body of a request message using Curl, you need to pass the data to Curl using the -d or --data command line switch. The Content-Type header indicates the data type in the body of the request message. This header is required by the server to correctly interpret and process the data in the body of the POST message. For example, if you send JSON to the server, you must specify the data type in the body using the Content-Type: application/json header. Click Run to execute Curl POST body example online and see results.
How do I post request body with Curl? Run
curl -X POST https://reqbin.com/echo/post/json 
   -H "Content-Type: application/json"
   -d '{"productId": 123456, "quantity": 100}'  
Updated: Viewed: 23269 times

Curl POST request syntax with a message body

The general form of a Curl command for making a POST request with a body is as follows:

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

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.

What is Curl?

Curl is a command-line utility for transferring data to or from a remote server using one of the supported protocols. Developers use curl to test API, send requests to the server, view server response headers, and load-test APIs. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, FTPS, and SFTP, has built-in support for SSL certificates, supports HTTP POST, HTTP PUT, FTP file upload, web form submission, user authentication, HTTP Cookies, and more.

What is HTTP POST request method?

HTTP POST is one of 9 common HTTP request methods supported by HTTP. The POST method requests the webserver to accept the data enclosed in the POST request message's body to process or store it. The HTTP POST method is used to upload files and submit web forms. 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.

Can I send data in the body of an HTTP POST request?

Yes, you can send any data to a server in the body of the HTTP POST request. HTTP POST requests need a Content-Type header that identifies the data type in the POST request body to allow the server to interpret and process this data correctly. For example, when submitting to a web form server, the Content-Type is usually application/x-www-form-urlencoded. When uploading files to the server, the Content-Type is usually multipart/form-data.

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]'

How to post the body of a message using Curl?

You can pass the body of the POST message to Curl with the -d or --data command-line option. Curl will send data to the server in the same format as the browser when submitting an HTML form. To send binary data in the body of a POST message with Curl, use the --data-binary command-line option. To send a file from the disk using Curl, start the data with the @ symbol; the rest of the parameter should be the file's name from which data will be read.

Curl POST Body
curl -d @file.name https://reqbin.com/echo/post/json

How to pass the Content-Type header using Curl?

When sending data, you must also pass the data type in the HTTP message body using the Content-Type request header. This is important and allows the server to interpret and process the message body correctly. The header can be passed to Curl using the -H command-line option.

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

Curl POST Body Examples

Example #1 Sending JSON in the body of a POST message:

Curl POST Body with JSON
curl -X POST https://reqbin.com/echo/post/json 
   -H "Content-Type: application/json"
   -d '{"productId": 1, "quantity": 10}'

Example #2 Sending HTML form in the body of a POST message:

Curl POST Body with HTML Form
curl -X POST https://reqbin.com/echo/post/form
   -H "Content-Type: application/x-www-form-urlencoded" 
   -d "param1=value1&param2=value2"

Example #3 Sending XML in the body of a POST message:

Curl POST Body with XML
curl -X POST https://reqbin.com/echo/post/xml
   -H "Content-Type: application/xml"
   -d "<Request><Login>my_login</Login><Password>my_password</Password></Request>"

Example #4 Sending File in the body of a POST message:

Curl POST Body with File
curl -X POST https://reqbin.com/echo/post -d @data.txt

Generate Code Snippets for Curl POST Body Example

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