Using Content-Length Header

The Content-Length header specifies the data size in the HTTP message's body. Browsers automatically add Content-Length and Content-Type headers based on the size and type of data sent to the server. To manually pass the Content-Length header to the server, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describes the size and type of data in the body of the POST request. The data length must be specified in bytes. In this Content-Length Header Example, we make a POST request to the ReqBin echo URL and send data to the server in the body of the POST message. Click Send to run Content-Length Header example online and see the results.
Using Content-Length Header Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 473

{"widget": {
    "window": {
        "title": "Sample Widget",
        "name": "sample_widget",
        "width": 600,
        "height": 400
    },
    "image": { 
        "src": "images/test.png",
        "name": "sample_test",
        "hOffset": 150,
        "vOffset": 150,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "sample_click",
        "alignment": "center"
    }
}} 
Updated: Viewed: 74405 times

What is HTTP?

Hypertext Transfer Protocol is an application-layer protocol for transferring data over the network, which is the core of the WWW (World Wide Web) and provides communication between HTTP clients (browser or mobile application) and servers. HTTP is built around messages called request and response. The client sends a request to the server, then the server processes this request and sends a response back to the client. Any HTTP message consists of a request/response line, HTTP headers, and a message body.

What is a POST request?

The HTTP POST method requests the webserver to receive and process the data enclosed in the body of the POST message. The POST is one of the nine standard HTTP methods. The POST method is used to send data to the server, upload files and images, as well as for and send HTML forms. POST requests differ from GET and HEAD requests in that they can change the state of the server.

What does the Content-Length header mean?

A Content-Length header is a number that indicates the size of the data in the body of the request or response in bytes. The HTTP body begins immediately after the first blank line, after the initial line and headers. The actual length of the content sent over the network may differ from the size of the data in the body because servers can compress the data before sending it.

Content-Length Header Syntax

The following is the common syntax for Content-Length Header:

Content-Length Syntax
Content-Length: [length]

Where:
  • length: the length of the message body is specified in bytes.

How to add a Content-Length header to the POST request?

Clients (browsers, mobile apps) automatically add a Content-Length header to the POST request based on the size of your data in the request. In this example, the Content-Length header indicates the size of the provided JSON.

POST Request With Content-Length Header Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 52

{
  "Id": 78912,
  "Quantity": 1,
  "Price": 18.00
}

For example, if you upload a PDF file of 100kb, the browser will send an HTTP POST request and add the Content-Length: 102400 and "Content-Type: application/pdf" headers.

See also

Generate Code Snippets for POST Request Content Length Header Example

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