What's the "Content-Length" field in HTTP header?

Http Headers

Http Headers Problem Overview


What does it mean?

  1. Byte count of encoded content string with encoding specified in header.
  2. Character count of content string.

Especially in case of Content-Type: application/x-www-form-urlencoded.

Http Headers Solutions


Solution 1 - Http Headers

It's the number of bytes of data in the body of the request or response. The body is the part that comes after the blank line below the headers.

Solution 2 - Http Headers

rfc2616

> The Content-Length entity-header field indicates the size of the > entity-body, in decimal number of OCTETs, sent to the recipient or, in > the case of the HEAD method, the size of the entity-body that would > have been sent had the request been a GET.

It doesn't matter what the content-type is.

Extension at post below.

Solution 3 - Http Headers

The Content-Length header is a number denoting an the exact byte length of the HTTP body. The HTTP body starts immediately after the first empty line that is found after the start-line and headers.

Generally the Content-Length header is used for HTTP 1.1 so that the receiving party knows when the current response* has finished, so the connection can be reused for another request.

* ...or request, in the case of request methods that have a body, such as POST, PUT or PATCH

Alternatively, Content-Length header can be omitted and a chunked Transfer-Encoding header can be used.

If both Content-Length and Transfer-Encoding headers are missing, then at the end of the response the connection must be closed.

The following resource is a guide that I found very useful when learning about HTTP:

HTTP Made Really Easy.

Solution 4 - Http Headers

One octet is 8 bits. Content-length is the number of octets that the message body represents.

Solution 5 - Http Headers

From here:

> The Content-Length entity-header field > indicates the size of the entity-body, > in decimal number of OCTETs, sent to > the recipient or, in the case of the > HEAD method, the size of the > entity-body that would have been sent > had the request been a GET. > > Content-Length = "Content-Length" ":" 1*DIGIT > An example is > > Content-Length: 3495 > Applications SHOULD use this field to > indicate the transfer-length of the > message-body, unless this is > prohibited by the rules in section > 4.4. > > Any Content-Length greater than or > equal to zero is a valid value. > Section 4.4 describes how to determine > the length of a message-body if a > Content-Length is not given. > > Note that the meaning of this field is > significantly different from the > corresponding definition in MIME, > where it is an optional field used > within the "message/external-body" > content-type. In HTTP, it SHOULD be > sent whenever the message's length can > be determined prior to being > transferred, unless this is prohibited > by the rules in section 4.4.

My interpretation is that this means the length "on the wire", i.e. the length of the *encoded" content

Solution 6 - Http Headers

From this page

> The most common use of POST, by far, > is to submit HTML form data to CGI > scripts. In this case, the > Content-Type: header is usually > application/x-www-form-urlencoded, > and the Content-Length: header gives > the length of the URL-encoded form > data (here's a note on URL-encoding). > The CGI script receives the message > body through STDIN, and decodes it. > Here's a typical form submission, > using POST: > > > > POST /path/script.cgi HTTP/1.0 > From: [email protected] > User-Agent: HTTPTool/1.0 > Content-Type: application/x-www-form-urlencoded > Content-Length: 32

Solution 7 - Http Headers

According to the spec:

> The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET. > > Content-Length = "Content-Length" ":" 1*DIGIT > > An example is > > Content-Length: 3495 > >Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4. > >Any Content-Length greater than or equal to zero is a valid value. Section 4.4 describes how to determine the length of a message-body if a Content-Length is not given. > >Note that the meaning of this field is significantly different from the corresponding definition in MIME, where it is an optional field used within the "message/external-body" content-type. In HTTP, it SHOULD be sent whenever the message's length can be determined prior to being transferred, unless this is prohibited by the rules in section 4.4.

Solution 8 - Http Headers

Consider if you have headers such as:

content-encoding: gzip
content-length: 52098
content-type: text/javascript; charset=UTF-8

The content-length is the size of the compressed message body, in "octets" (i.e. in units of 8 bits, which happen to be "bytes" for all modern computers).

The size of the actual message body can be something else, perhaps 150280 bytes.

The number of characters can be different again, perhaps 150231 characters, because some unicode characters use multiple bytes (note UTF-8 is a standard encoding).

So, different numbers depending on whether you care how much data is transmitted, or how much data is held, or how many symbols are seen. Of course, there is no guarantee that these headers will be provided..

Solution 9 - Http Headers

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

>Content-Length = "Content-Length" ":" 1*DIGIT

An example is

>Content-Length: 1024

Applications SHOULD use this field to indicate the transfer-length of the message-body.

In PHP you would use something like this.

header("Content-Length: ".filesize($filename));

In case of "Content-Type: application/x-www-form-urlencoded" the encoded data is sent to the processing agent designated so you can set the length or size of the data you are going to post.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneonilView Question on Stackoverflow
Solution 1 - Http HeadersTom CabanskiView Answer on Stackoverflow
Solution 2 - Http HeadersWhirlWindView Answer on Stackoverflow
Solution 3 - Http HeadersspenderView Answer on Stackoverflow
Solution 4 - Http HeadersitsprojectView Answer on Stackoverflow
Solution 5 - Http HeadersDaniel RenshawView Answer on Stackoverflow
Solution 6 - Http HeadersTheresaView Answer on Stackoverflow
Solution 7 - Http HeadersBozhidar BatsovView Answer on Stackoverflow
Solution 8 - Http HeadersbenjiminView Answer on Stackoverflow
Solution 9 - Http HeadersGaurav JassalView Answer on Stackoverflow