POST on HTTP Server

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
fastlink30
Posts: 3
Joined: Sun Dec 18, 2016 2:35 pm

POST on HTTP Server

Post by fastlink30 » Sun Dec 18, 2016 3:41 pm

from an HTTP server is possible get the body of a POST HTTP request? the example i found are only with GET.
thanks

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: POST on HTTP Server

Post by deshipu » Sun Dec 18, 2016 7:50 pm

Yes.

fastlink30
Posts: 3
Joined: Sun Dec 18, 2016 2:35 pm

Re: POST on HTTP Server

Post by fastlink30 » Sun Dec 18, 2016 7:58 pm

[quote="deshipu"]Yes.[/quote]

ok, thanks for the reply, but how?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: POST on HTTP Server

Post by deshipu » Sun Dec 18, 2016 8:04 pm

Just read it from the socket, like you did with the headers. You might want to refer to the HTTP protocol specification for details.

User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: POST on HTTP Server

Post by ernitron » Fri Dec 23, 2016 11:48 am

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'..

fastlink30
Posts: 3
Joined: Sun Dec 18, 2016 2:35 pm

Re: POST on HTTP Server

Post by fastlink30 » Fri Dec 23, 2016 5:48 pm

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

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

Re: POST on HTTP Server

Post by markxr » Wed Jan 04, 2017 7:42 pm

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: POST on HTTP Server

Post by pythoncoder » Thu Jan 05, 2017 7:35 am

@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.
Peter Hinch
Index to my micropython libraries.

Post Reply