9

I am trying to build an HTTP server with an AVR + ESP8266.

I can send commands back and forth via telnet, but now I want to implement a web interface.

As a starting point I tried to setup a website that outputs "text" however, the browser displays an empty page. Can someone please let me know the minimum requirements for the page to be interpreted as HTML?

telnet 192.168.2.26 81
Trying 192.168.2.26...
Connected to 192.168.2.26.
Escape character is '^]'.

GET / HTTP/1.1

AVR answer:

HTTP/1.1 200 OK
Content-Type: text/html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Zeitschaltuhr</title></head>
<body>
Text
</body></html>
Connection closed by foreign host.
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Sebastian Heyn
  • 387
  • 3
  • 15
  • What do you see when viewing the page source in the browser? (ctrl-u) – sdabet Nov 18 '15 at 15:40
  • empty page, only in front of line 1 there's a "1" – Sebastian Heyn Nov 18 '15 at 15:42
  • The problem is not in your HTML syntax. If you just returned "Text" without any HTML markup the browser would display it. The issue is that your browser receives nothing at all. – sdabet Nov 18 '15 at 15:44
  • Thats kinda strange, as it works from the same box, using telnet. BTW: if I delete the first to lines from the reply, the html is displayed, but not interpreted – Sebastian Heyn Nov 18 '15 at 15:45
  • Which port is your web server listening on? What URL do you put in the browser? – sdabet Nov 18 '15 at 15:46
  • port 81, and I connect 192.168.2.26:81 - I can see the reply and everything on the uart logger so that works. – Sebastian Heyn Nov 18 '15 at 15:46
  • In the browser's web dev tools, can you see the response received by the browser? does it actually receive a 200 status code? – sdabet Nov 18 '15 at 15:48
  • You might have to add an empty line between HTTP headers and body, see my answer. – sdabet Nov 18 '15 at 16:07

3 Answers3

12

Minimal HTTP response:

HTTP/1.1 404 
Content-Length: 0

Minimal response with content:

HTTP/1.1 200 OK
Content-Length: 12
Content-Type: text/plain; charset=utf-8

Hello World!

The reason it does not work for you is that you forgot the Content-Length: header.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Zibri
  • 9,096
  • 3
  • 52
  • 44
  • 1
    Actually if you use a program and have control over terminating the connection and you are connecting through a chrome browser client, you don't need to specify length. In general you don't need to specify Content-Type either when connecting with chrome browser, although its probably good practice to specify both. – yosefrow Jul 09 '20 at 15:10
  • yep @yosefrow it's good practice to add it for browser compatibility – Zibri Jul 12 '20 at 12:20
  • 3
    Why Length is 13 and not 12? There is new line in the end? – ScienceDiscoverer Mar 27 '21 at 19:47
  • @ScienceDiscoverer I edited the answer because this confused me too. I checked with a simple [Flask](https://flask.palletsprojects.com/) server and it sets Content-Length to 12 if there's no newline. I also thought it maybe should be 14 since HTTP uses `\r\n` instead of just `\n` but apparently you can send whatever in the response body. – Boris Verkhovskiy Feb 16 '23 at 11:10
  • In HTTP/1.0 you can leave the `Content-Length` header out and it is according to spec. For HTTP/1.1 you need either `Content-Length` or `Transfer-Encoding: chunked` — the latter of which requires you to send the body in a special format, but then you don't need to know the amount of bytes in advance. In your case, HTTP/1.0 would probably be the most suitable solution. – Jonas Berlin Mar 02 '23 at 12:20
2

Your HTTP response is missing the empty line between the response header fields and the message body (as explained here):

HTTP/1.1 200 OK
Content-Type: text/html

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Zeitschaltuhr</title></head>
<body>
Text
</body></html>
sdabet
  • 18,360
  • 11
  • 89
  • 158
2

Important to provide twice CR,LF before content.

HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/html\r\n\r\nHello World!
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
EVGENII
  • 21
  • 1