Posting Request Body with Curl [Curl/Bash Code]

To send data to the server in the POST request body, you must pass the required data to Curl using the -d or --data command line switch and specify the data content type using the -H command line switch. The Content-Type header is required for the server to correctly interpret and process the data in the body of the POST message. For example, if you send JSON to a server, you must specify the content type of the data in the body of the POST message using the Content-Type header: application/json and application/xml for XML. Click Run to execute Curl POST Body example online and see results. The Curl/Bash code was automatically generated for the Curl POST Body example.
Posting Request Body with Curl [Curl/Bash Code] Run
curl -X POST https://reqbin.com/echo/post/json 
   -H "Content-Type: application/json"
   -d '{"productId": 123456, "quantity": 100}'  
Updated: Viewed: 37692 times
Curl/Bash code for Curl POST Body example

Curl/Bash code for Curl POST Body Example

This Curl/Bash code snippet was generated automatically for the Curl POST Body example.
<< Back to the Curl POST Body example

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 body?

The HTTP body is the data sent in an HTTP message to the other side during client-server communication. Not all HTTP messages have a body, such as GET, HEAD, DELETE, or OPTIONS, sent by clients to the server. When submitting requests to update resources on the server, clients usually send resource data in a message body, such as with a POST request containing HTML form data.

HTTP body can be divided into two categories:

1. Single resource bodies: consisting of a single file, are identified by two headers: Content-Length and Content-Type.

2. Multiple-resource bodies: containing multiple parts, each with a different piece of information (usually applied to HTML form).

HTTP Body MIME Types

Multipurpose Internet Mail Extensions (MIME) is a fundamental part of communication protocols like HTTP. When you need to transfer non-text data, you need the MIME type. The MIME type consists of three parts: type, subtype, and optional parameters, separated by a forward slash "/" without spaces. MIME types are not case-sensitive and are usually written in lowercase. MIME types must have a type and a subtype. Each type has a set of subtypes. Following is the MIME type syntax for different data types in the message body::

MIME Types Syntax
Content-Type: application/json

Content-Type: application/xml

Content-Type: application/x-www-form-urlencoded

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

The following are examples of sending a Curl POST body:

Sending JSON in the body of a POST message

The following is an example of sending JSON in the body of a POST message:

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

Sending HTML form in the body of a POST message

The following is an example of submitting an HTML form in the body of a POST message:

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

Sending XML in the body of a POST message

The following is an example of sending XML in the body of a POST message:

Curl POST Body with XML Example
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>"

Sending File in the body of a POST message

The following is an example of sending a file in the body of a POST message:

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

See also

Generate code snippets for Curl/Bash and other programming languages

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