Reading body from POST request on ESP8266
Posted: Thu Sep 09, 2021 3:09 am
I originally found this thread: viewtopic.php?t=2793
and followed the idea described there. However for it doesn't work for me.
Here is a code I wrote:
and are defined as following:
Somehow this code returned body content properly few times but after reconnecting the board "content length" gets extracted correctly but returns a fragment of headers like
Could you please help me to figure out what do I do wrong here?
and followed the idea described there. However for it doesn't work for me.
Here is a code I wrote:
Code: Select all
server = self.__httpServer
server.listen(0)
while self.__runServer:
request, _ = server.accept()
headers = request.recv(REQUEST_FRAME_SIZE) # REQUEST_FRAME_SIZE here is 1024
(method, url) = self.__parseUrl(headers)
print('Request:', method, url)
if method == 'POST':
contentLength = self.__readContentLength(headers)
body = self.__readBody(contentLength, request)
Code: Select all
__readContentLength
Code: Select all
__readBody
Code: Select all
def __readContentLength(self, headers):
requestText = repr(headers)
print('Headers: ', requestText)
matchObject = self.__contentLengthRegexp.match(requestText)
contentLength = matchObject.group(CONTENT_LENGTH_GROUP)
return int(contentLength.strip())
def __readBody(self, contentLength, request):
print('Content length: ', contentLength)
bodyContentString = request.read(contentLength).decode(REGULAR_STRING_ENCODING)
print('Content: ', bodyContentString)
return parseBody(bodyContentString)
Code: Select all
request.read(contentLength)
Code: Select all
92.168.0.1/
Accept-Encoding: gzip