70

Is the Content-Length header required for a HTTP/1.0 response? The HTTP spec mentions that it is required for the request, but doesn't mention anything about the response:

http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#Content-Length

A valid Content-Length field value is required on all HTTP/1.0 request messages containing an entity body.

If it is not required for the response, how does the client read the response when it's larger than 1MB?

Sean Nguyen
  • 12,528
  • 22
  • 74
  • 113

1 Answers1

71

Section 10.4 of the spec (which you linked to) doesn't say anything about requirements on responses itself, but instead links to section 7.2.2, which specifies that the server can indicate the length of a response containing an entity body by

  • sending a Content-Length header, or
  • closing the connection when the entire response has been sent.

Section 7.2 says that responses to HEAD requests, and 1xx, 204 or 304 responses, should not include an entity body, and therefore need not include a Content-Length header; and

All other responses must include an entity body or a Content-Length header field defined with a value of zero (0).

So to answer the question: When no Content-Length is received, the client keeps reading until the server closes the connection.

  • 4
    The conclusion in the end is not completely accurate I believe. "The client keeps reading until the server closes the connection, or the announced content-length has been received; After which the (tcp) connection can be used for another request." Let me know if I'm making mistakes here. – dot slash hack Aug 12 '14 at 22:01
  • @KemHeyndels Well, the question basically asked what happened when there *wasn't* a `Content-Length` header, so… – michaelb958--GoFundMonica Aug 12 '14 at 22:02
  • 2
    @KemHeyndels I've clarified the answer a little; it *was* written in my newbie days, and perhaps a bit unclear. Thanks for bringing it to my attention. – michaelb958--GoFundMonica Aug 12 '14 at 22:06
  • I learned (the hard way) that if a response is "large," including Content-Length can prevent issues. Not sure what "large" is, but it my API started working for responses over 100k when I included it. – ScottyB Jan 03 '18 at 13:53
  • @ScottyB Makes sense - probably something was defaulting to a 100k buffer. – michaelb958--GoFundMonica Jan 03 '18 at 15:02
  • 1
    especially on android, having Content-Length is mandatory. I learnt that the hard way : https://stackoverflow.com/questions/51685816/android-could-not-receive-form-submit-response Without the Content-Type, android gives an exception if server closes connection (reads the data, then shouts exception). Problem went away as soon as Content-Type was put. Its one of those non standard things which different libraries may want to be mandatory, so its always better to put it, even if rfc doesnt mandate it – Ouroboros Aug 07 '18 at 06:27