Reading body from POST request on ESP8266

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
donkihot
Posts: 1
Joined: Thu Sep 09, 2021 2:57 am

Reading body from POST request on ESP8266

Post by donkihot » 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:

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
and

Code: Select all

__readBody
are defined as following:

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)
 
Somehow this code returned body content properly few times but after reconnecting the board "content length" gets extracted correctly but

Code: Select all

request.read(contentLength)
returns a fragment of headers like

Code: Select all

 
 92.168.0.1/
Accept-Encoding: gzip
Could you please help me to figure out what do I do wrong here?

Post Reply