HTTP response without content-length doesn't work

  • Thread starter Swamp Thing
  • Start date
  • #1
Swamp Thing
Insights Author
919
616
I am trying out the web server feature on a NodeMCU ESP8266 board. If I include a Content-Length line in the response header, it works perfectly.

But if I omit the content-length, then Chrome and Firefox fail to display the page. Chrome displays a blank page, and Firefox says "Connection was reset".

This is the HTTP response as received on Telnet:
Code:
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close

<html><body>Hello</body></html>
Connection closed by foreign host.
(There is a "\r\n" at the end of each line and after the header).

Curl and Wget also report "connection closed" errors.

My understanding is that if content-length is not specified, then closing the connection is a valid way to signal end of data. Especially with "connection: close". So what is the problem?
 
Technology news on Phys.org
  • #2
  • Like
Likes Swamp Thing

Related to HTTP response without content-length doesn't work

Why does an HTTP response without a Content-Length header not work?

The Content-Length header informs the client about the size of the response body in bytes. Without it, the client doesn't know when the response ends, leading to potential issues in processing the response. This can result in incomplete data being read or the connection being kept open indefinitely.

What are the alternatives to using the Content-Length header?

An alternative to the Content-Length header is the Transfer-Encoding: chunked header. This allows the server to send the response in chunks, each with its own size indicator, so the client knows when the response has ended without needing a total content length upfront.

How can I troubleshoot an HTTP response issue related to missing Content-Length?

To troubleshoot, you can use tools like curl or Postman to inspect the headers of the HTTP response. Check if the Content-Length or Transfer-Encoding headers are present. Additionally, review server logs for any errors related to response handling and ensure that your server configuration correctly sets these headers.

Is it mandatory to include the Content-Length header in all HTTP responses?

It is not mandatory to include the Content-Length header in all HTTP responses, but it is highly recommended for responses with a known length to avoid issues. For dynamic or streaming content, Transfer-Encoding: chunked is often used instead.

Can HTTP/2 handle responses without a Content-Length header better than HTTP/1.1?

Yes, HTTP/2 can handle responses without a Content-Length header more efficiently than HTTP/1.1. HTTP/2 uses a binary framing layer, which allows for more efficient and reliable message parsing, reducing the dependency on the Content-Length header for determining message boundaries.

Similar threads

  • Programming and Computer Science
Replies
1
Views
672
  • Programming and Computer Science
Replies
7
Views
3K
Replies
31
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Computing and Technology
Replies
1
Views
7K
  • Computing and Technology
Replies
4
Views
3K
  • Special and General Relativity
3
Replies
75
Views
4K
Back
Top