Page 1 of 1

HTTP server body

Posted: Sat Sep 09, 2017 4:43 pm
by wipity
Hi , i'm trying to use a web server example from micropython on esp8266, but I don't understand how to read the body of a POST request .
I send the request from Postman and I receive all the header.
I've try to check if Content-Length is present to print the query.
I receive the correct length of the body but if I try to print it , it's empty.
How Can I read it?
Thanks

Re: HTTP server body

Posted: Mon Sep 11, 2017 1:33 pm
by wipity
Hi,
I solved checking the len of the response and printing only the part after '\r\n\r\n' , the body of a POST.
Now my queston is: what is the maximum length possible to receive?
Now I have '.recv(4096)' and the len of the entire response is 536, the len of the body is 52. All outside of 52 character send in the body doesn't appear and I receive an error on Postman response.
this is the script, taken from the example of Micropython fw:

Code: Select all

try:
    import usocket as socket
except:
    import socket
CONTENT = b"""\
HTTP/1.0 200 OK

Hello #%d from MicroPython!
"""
def main():
    s = socket.socket()
    ai = socket.getaddrinfo("0.0.0.0", 8080)
    addr = ai[0][-1]

    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    s.bind(addr)
    s.listen(5)
    print("Listening, connect your browser to http://192.168.4.1:8080/")

    counter = 0
    while True:
        res = s.accept()
        client_s = res[0]
        client_addr = res[1]
        req = client_s.recv(4096)
        print("Request:")
        stringToFind = b"\r\n\r\n"
        stringSeek = req.find(stringToFind)
        startToReadBody = stringSeek+4
        if stringToFind in req:
            BodyToPrint = (req[startToReadBody:])
            print(BodyToPrint)
            print("request len",len(req))
            print("body len: ",len(BodyToPrint))
        client_s.send(CONTENT % counter)
        client_s.close()
        counter += 1
        print()


main()

this is the request from Postman:

Code: Select all

POST  HTTP/1.1
Host: 192.168.4.1:8080
Cache-Control: no-cache
Postman-Token: 202b97f1-4b83-ac8c-a5c6-224e3b435125

1234567890a1234567890b1234567890c1234567890d123456789
and the response is :

Code: Select all

Request:
b'1234567890a1234567890b1234567890c1234567890d12345678'
request len 536
body len:  52
as you can see, the last character was truncated, so how much character I can send?
thank you in advance

Re: HTTP server body

Posted: Tue Sep 12, 2017 7:43 am
by pythoncoder
Are you seeing an absolute limit on the length, or is the last character being lost regardless of length?

Re: HTTP server body

Posted: Tue Sep 12, 2017 10:36 am
by wipity
What I see is an absolute limit on the length, if I add character is always truncated at 52 of body .
The entire response of 536 includes all the header, token from Postman, body, etc.
I notice that in the 'socket.recv(bufsize)' I can decide the size of buffer to receive, but not more than 4096?
4096 is 4K, correct?

Re: HTTP server body

Posted: Tue Sep 12, 2017 12:32 pm
by pythoncoder
4096 is indeed 4KB. When reading my comments bear in mind that I have no actual experience of server code ;)

I don't know why you're seeing this limit. It's not obvious to me what causes the socket.recv() call to terminate, either in theory or in practice. Have you looked at the example in the docs http://docs.micropython.org/en/latest/e ... k_tcp.html which uses a slightly different technique? It is clear to me how that works.

Another reference is this https://github.com/fadushin/esp8266/tre ... hon/uhttpd. While it may be overkill for your application it does look like an excellent approach and it's very well documented. I plan to play with it to learn about this server stuff.