What's a reliable HTTP response for ESP8266 Web Server?

Hi All, I suspect this is more of a web development issue than to do with Arduino but here it is anyway... I trust you lot would have a better understanding than most...

I have an ESP8266 with a web server hosting a very simple page. The page just returns a simple JSON response. The response page seems to work very well in Edge, quite often in IE and rarely ever in Chrome, Fireforx or my android mobile.

Can you see what's wrong with the page that generates from this?

This is the page string I return:

  String htmlPage = "" +
      String("HTTP/1.1 200 OK\r\n") +
      "Content-Type: application/json\r\n" +
      "\r\n" +
      "{ ""digitalPin1Toggle"" : """ + digitalPin1Toggle +""" }\r\n";

I expect something like:

{ digitalPin1Toggle : Off }

Displays OK on Edge

On Chrome I mostly get "This site can’t be reached" and sometimes just a blank page. When I Inspect the page in Chrome, there's a console error "GET http://10.1.1.157/D1/Off net::ERR_CONNECTION_RESET 200 (OK)"

Firefox always returnd "The connection was reset".

My mobile browser sometimes displays the response page and sometimes is totally blank.

But in all cases, I know the request was received by the ESP and it responded. BTW, I also tried just a plain ordinary HTML page with the same result.

Thanks for your help...
Cheese

add "Content-length:" header and "Connection: close" header

1 Like

Good one. Thanks Juraj! "Content-Length:" did the job. I just had to refactor the code to do the calculation.