POST Request With Content-Length Header [Java Code]

When making a POST request, HTTP clients typically automatically add a Content-Length header based on the size of the data supplied and a Content-Type header based on the type of data being transferred. To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request. In this POST request with a Content-Length Header Example, we make a POST request to the ReqBin echo URL and send the JSON data to the server. Click Send to make a POST request with Content-Length Header online and see the results. The Java code was automatically generated for the POST Request Content Length Header example.
POST Request With Content-Length Header [Java Code] 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: 21569 times
Java code for POST Request Content Length Header example

Java code for POST Request Content Length Header Example

This Java code snippet was generated automatically for the POST Request Content Length Header example.
<< Back to the POST Request Content Length Header example

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 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 Java and other programming languages

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