Reputation: 56
I am writing a lua script to send some data to a webapp I developed using spring-boot from my ESP8266 WeMOS LoLin board. To do so, the script has to authenticate on the webapp first. The problem is when I POST the authentication data, I find in my server logs that even the authentication is done correctly, the session closed.
Here is part of my lua code
print("Authenticating .........")
local url = getBaseUrl() .. '/login'
local body = 'username=' .. config.server.usr .. '&'..
'password=' .. config.server.pwd .. '&' ..
'X-CSRF-TOKEN=a65sd464-6666-4bb4-4543-23k234tl234'
local headers =
'Content-Type: application/x-www-form-urlencoded\r\n'..
'Connection: keep-alive\r\n'..
'Accept: */*\r\n' ..
'Cookie: JSESSIONID=F7A9D7FA7D9AF79D7F9ASD7FA97A979F7D7A'
print(url, "\n", headers, "\n", body)
http.post(url, headers, body, loginPostCallback)
*X-CSRF-TOKEN and JSESSIONID in this example are dummy values. In the full script they are got from the response of a previous GET request
So I tried doing the same operation using curl
from command line, and I had no problem at all.
curl -v -H "Content-Type: application/x-www-form-urlencoded\r\nConnection: keep-alive\r\nAccept: */*\r\nJSESSIONID=F7A9D7FA7D9AF79D7F9ASD7FA97A979F7D7A" -b "JSESSIONID=F7A9D7FA7D9AF79D7F9ASD7FA97A979F7D7A" -d "X-CSRF-TOKEN=a65sd464-6666-4bb4-4543-23k234tl234&username=admin&password=admin" http://192.168.1.4:8080/login
Then, I traced on the server the requests, and compared what sends my lua script with what is sent by curl, and I saw lua http.post()
was always sending a "Connection: close"
header, even when I am explicitly setting the "Connection: keep-alive"
header -which is also included-.
Looking at the NodeMCU http library code I saw in http.c, line 224 is always including the "Connection: close"
header.
Does anyone know why are they doing it? Is there any way to make "Connection: keep-alive"
requests?
Thanks in advance
I've been able to authenticate in the server doing a workaround, using the net
library instead of the http
and sending Connection: keep-alive
headers. Anyway, my questions remain still unanswered so, unless the admins tell me to publish my workaround as a solution and mark the question as resolved, I will leave it open waiting for someone to answer.
Upvotes: 0
Views: 714
Reputation: 23535
At least it's documented but the behavior has been like that from day 1. As I didn't write that code I can only speculate as to the "why". Hence, the question doesn't fit well with the Stack Overflow Q&A style.
The ESP8266 is a very constrained device; memory- and otherwise. Not keeping HTTP connections alive until they time out or are closed by the server, therefore, does make sense.
Upvotes: 1