Page 1 of 1

POST on HTTP Server

Posted: Sun Dec 18, 2016 3:41 pm
by fastlink30
from an HTTP server is possible get the body of a POST HTTP request? the example i found are only with GET.
thanks

Re: POST on HTTP Server

Posted: Sun Dec 18, 2016 7:50 pm
by deshipu
Yes.

Re: POST on HTTP Server

Posted: Sun Dec 18, 2016 7:58 pm
by fastlink30
[quote="deshipu"]Yes.[/quote]

ok, thanks for the reply, but how?

Re: POST on HTTP Server

Posted: Sun Dec 18, 2016 8:04 pm
by deshipu
Just read it from the socket, like you did with the headers. You might want to refer to the HTTP protocol specification for details.

Re: POST on HTTP Server

Posted: Fri Dec 23, 2016 11:48 am
by ernitron
fastlink30 wrote:from an HTTP server is possible get the body of a POST HTTP request? the example i found are only with GET.
thanks
Hi, I use something like:

Code: Select all

while True:
           try:
               self.conn, self.addr = self.socket.accept()
               req = self.conn.readline()
           except KeyboardInterrupt:
               raise OSError('Interrupt')
           except Exception as e:
               print(e)
               return

           l = 0
           while True:
               h = self.conn.readline()
               if not h or h == b'\r\n':
                   break
               if 'Content-Length: ' in h:
                   try:
                       l = int(h[16:-2])
                       print ('Content Length is : ', l)
                   except:
                       continue

           if l :
               postquery = self.conn.read(l)
               print(postquery)
Forgive me, but is an excerpt from my httpserver class, but I guess you understand the overall picture.

The most important thing is to get the content length from header. Once you have that just read the number of bytes from socket. Et voila'..

Re: POST on HTTP Server

Posted: Fri Dec 23, 2016 5:48 pm
by fastlink30
thank you ernitron, this help me, now i try to make the http server asyncronous, i read many posts but is little difficult, i think must use uasyncio from the micropython-lib repository, but i not see example to understand how must use and explain this library

Re: POST on HTTP Server

Posted: Wed Jan 04, 2017 7:42 pm
by markxr
It is easy enough to do, you will probably want to check the Content-length header (maybe reject posts that don't have it).

If the POST is small enough to fit in ram, then it's no problem. Otherwise, you'll need to read it in chunks and parse out. I've implemented HTTP file upload with PUT, because I don't want to parse multipart-form-data POSTs.

Mark

Re: POST on HTTP Server

Posted: Thu Jan 05, 2017 7:35 am
by pythoncoder
@fastlink30 Re uasyncio I have started to write a tutorial on the MicroPython subset of asyncio: see https://github.com/peterhinch/micropython-async and follow the link. It's aimed more at hardware interfacing than networking applications, but it should give you the basics.