from an HTTP server is possible get the body of a POST HTTP request? the example i found are only with GET.
thanks
POST on HTTP Server
-
- Posts: 3
- Joined: Sun Dec 18, 2016 2:35 pm
Re: POST on HTTP Server
[quote="deshipu"]Yes.[/quote]
ok, thanks for the reply, but how?
ok, thanks for the reply, but how?
Re: POST on HTTP Server
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
Hi, I use something like: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
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)
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'..
-
- Posts: 3
- Joined: Sun Dec 18, 2016 2:35 pm
Re: POST on HTTP Server
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
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
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
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: POST on HTTP Server
@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.
Index to my micropython libraries.