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

asked Nov 13, 2018 at 20:06

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.

answered Nov 13, 2018 at 20:08
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. Commented 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?) Commented Nov 14, 2018 at 12:05
  • perferct. It works printing always the string.length() in Content-Length. Thanks Commented Nov 15, 2018 at 14:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.