Reputation: 86145
What does it mean?
Especially in case of Content-Type: application/x-www-form-urlencoded
.
Upvotes: 367
Views: 717449
Reputation: 4910
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..
Upvotes: 13
Reputation: 14110
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.
Upvotes: 287
Reputation: 120528
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:
Upvotes: 94
Reputation: 56665
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.
Upvotes: 9
Reputation: 34187
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
Upvotes: 35
Reputation: 912
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.
Upvotes: 6
Reputation: 955
One octet is 8 bits. Content-length is the number of octets that the message body represents.
Upvotes: 62
Reputation: 3645
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
Upvotes: 12
Reputation: 8018
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.
Upvotes: 327