302

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.

pevik
  • 3,746
  • 3
  • 27
  • 37
eonil
  • 75,400
  • 74
  • 294
  • 482

9 Answers9

275

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.

Tom Cabanski
  • 7,390
  • 2
  • 20
  • 25
  • 1
    Rather than "request or response", isn't it "the body of the response" ONLY? The request doesn't have a blank line between header and data. – ajfbiw.s Feb 29 '16 at 08:08
  • can I change the HTTP response header content length filed in a java filter – KItis Sep 29 '16 at 09:50
  • 2
    @ajfbiw.s Consider a http post request. "The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request's message-headers. " from https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3 – Roy Guanyu Oct 23 '16 at 08:46
251

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.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
WhirlWind
  • 13,246
  • 2
  • 39
  • 41
  • 17
    Combine this answer with the answer from Tom Cabanski and you have all the information you need. In case of text you can count the number of characters since ASCII is 8 bit. – hcpl Sep 24 '12 at 08:45
  • 10
    @hcpl: but text is not necessarily always ASCII, also ASCII is 7-bit, not 8. – Lie Ryan Mar 08 '13 at 10:24
  • 2
    Replace ASCII with whatever encoding you're using and look up the number of bits that encoding requires. For the 7 vs 8 bit response; Originally it was 7 bit indeed so you're right again. But today 8 bits are used in most (if not all) cases because of the way computer memory is organized. edit: Rereading my answer I see where you're going. I should rephrase my original comment. – hcpl Mar 10 '13 at 19:28
  • 8
    @hcpl: the entire reason why the bytes vs. characters issue is significant is due to variable width encodings like UTF-8 and UTF-16 where the "number of bits" is not fixed. – StefanKarpinski Oct 06 '14 at 17:58
76

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.

spender
  • 106,080
  • 28
  • 202
  • 324
  • "Content-Length header can be omitted and a chunked Transfer-Encoding header can be used" : Literally saved my day. Thank you so much. – Berthim Mar 23 '21 at 14:57
49

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

itsproject
  • 795
  • 1
  • 6
  • 13
  • 79
    @ViniciusPires 'byte' is commonly used to describe smallest addressable unit of memory, which is not necessarily 8 bits on all architectures. That is why 'octet' is used to avoid any ambiguity – PJK Mar 29 '15 at 13:27
  • 6
    @PJK, Nope, not *exactly* right. `OCTET` here has a **very specific meaning** which means . See the definition at http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2 – Pacerier Jul 24 '15 at 13:57
30

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

Pacerier
  • 76,400
  • 86
  • 326
  • 602
Daniel Renshaw
  • 32,045
  • 8
  • 71
  • 91
  • 7
    "On the wire", the length would be altered depending on compression, but it is correct to say the length before being compressed. – BayssMekanique May 02 '13 at 21:51
  • Will Content-Length differs on different type of machines like Mac/Linux? OR will it differ while using different client like curl/postman ...? – Kanagavelu Sugumar Feb 23 '17 at 12:48
  • 1
    Assuming all else remains equal (e.g. encoding, compression, etc.) then the content length should be platform independent. This is a header from the server so, assuming it doesn't sniff the user-agent and behave differently, the client shouldn't make any difference. – Daniel Renshaw Feb 23 '17 at 17:13
9

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: frog@jmarshall.com
User-Agent: HTTPTool/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32
Frank Bollack
  • 22,354
  • 3
  • 45
  • 56
Theresa
  • 3,189
  • 9
  • 41
  • 45
6

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.

Community
  • 1
  • 1
Bozhidar Batsov
  • 52,348
  • 13
  • 95
  • 111
2

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.

Nakilon
  • 32,203
  • 13
  • 95
  • 132
Gaurav Jassal
  • 872
  • 1
  • 6
  • 6
2

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

benjimin
  • 2,128
  • 17
  • 35