Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens in HTTP response to a GET request without Content-Length or Transfer-encoding?

If a response to a GET request doesn't have the Content-Length or Transfer-encoding: chunked field, what happens? How does the client know when the message ends?

like image 306
user2378481 Avatar asked May 20 '15 03:05

user2378481


People also ask

DOES GET request have content-Length?

The Content-Length is optional in an HTTP request. For a GET or DELETE the length must be zero. For POST, if Content-Length is specified and it does not match the length of the message-line, the message is either truncated, or padded with nulls to the specified length.

What is HTTP GET response?

GET : The resource has been fetched and transmitted in the message body. HEAD : The representation headers are included in the response without any message body. PUT or POST : The resource describing the result of the action is transmitted in the message body.

Why is content-Length important in HTTP?

HTTP Content-Length entity-header is used to indicate the size of entity-body in decimal no of octets i.e. bytes and sent it to the recipient. It is a forbidden header name. Basically it is the number of bytes of data in the body of the request or response. The body comes after the blank line below the headers.

Do you need content-Length?

The Content-Length header is mandatory for messages with entity bodies, unless the message is transported using chunked encoding. Content-Length is needed to detect premature message truncation when servers crash and to properly segment messages that share a persistent connection.


1 Answers

RFC 7230 section 3.3.3 contains a nice checklist of conditions for finding the message body size. Essentially it says the answer depends on what the status code is. The relevant conditions being #1 and #7.

"1. Any response ... with a 1xx (Informational), 204 (No Content), or 304 (Not Modified) status code is always terminated by the first empty line after the header fields, regardless of the header fields present in the message, and thus cannot contain a message body."

"7. Otherwise, this is a response message without a declared message body length, so the message body length is determined by the number of octets received prior to the server closing the connection."

Its also worth knowing that the message may continue after the end of the body portion. RFC 7230 section 4.4 defines a Trailers feature where the payload may be followed by a second set of mime headers. If those exist the message ends where they do.

like image 105
Amos Jeffries Avatar answered Sep 28 '22 18:09

Amos Jeffries