1

I'm developing an Arduino server that should respond with a message when it receives a POST Request.

If I test my project with a HTTP Client (like insomnia or postman), I get an error:

Error: Failure when receiving data from the peer

I still get a response that I can read but with a final error.

That's my client request:

> POST /SOMETHING/ HTTP/1.1
> Host: [ARDUINO IP]
> User-Agent: insomnia/6.2.0
> Accept: */*
> Content-Length: 0

And this is my Arduino response considered as not valid:

< HTTP/1.1 200 OK
< Connection: close
< [MY MESSAGE]

* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 2

As you can notice, I just added 2 strings

HTTP/1.1 200 OK
Connection: close

before the real message.

Am I missing something to create a valid response?

Thanks

1 Answer 1

1

HTTP requires an empty line after HTTP headers.

First line is status line. Then the header lines. The headers are terminated by an empty line. Then the response body follows. Line terminator for HTTP is \r\n. Arduino println() function uses \r\n.

The response with body should contain Content-type and Content-length header. Alternative to Content-lenght is "chunked" Transfer-Encoding.

ESP8266WebServer library handles HTTP for you.

3
  • Thanks, it works. I just don't know why it still doesn't work in insomnia or postman even if it works if I read the request from a browser. Insomnia keeps saying: < HTTP/1.1 200 OK < Content-Type: text/html < Connection: close * Received 9 B chunk * Recv failure: Connection reset by peer * stopped the pause stream! ... looks like it receives the response but there's still the "recv failure" error.
    – GiuMex
    Nov 14, 2018 at 11:40
  • you don't send content length, so the client doesn't know where the body ends. it waits for more data and you close the connection. browser tolerates it or has some orientation in the body structure (html tags?)
    – Juraj
    Nov 14, 2018 at 12:05
  • perferct. It works printing always the string.length() in Content-Length. Thanks
    – Giumex
    Nov 15, 2018 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.