GET requests

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
jarekd
Posts: 13
Joined: Mon Aug 21, 2017 3:54 pm

GET requests

Post by jarekd » Sun Sep 23, 2018 11:04 pm

Hi :)
I don't want to re-invent the wheel (polish proverb), so I'm posting: is there some nice way (library?) of handling this kind of GET requests:

Code: Select all

GET /smooth?power=300&balance=0.05&time=5.0 HTTP/1.1
What I'm trying to do: I have web server on ESP8266. If you enter it's address in web browser with added data, it will change some variables / values.

Code: Select all

http://192.168.1.xxx/smooth?power=10&time=0.7
I have working prototype of extracting those values (with A LOT of string/array slicing/converting/stripping/...) but still I would need to add a lot more (to check if values correct type, and so on).
Any easier solution?

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: GET requests

Post by SpotlightKid » Mon Sep 24, 2018 5:43 pm

Install this module to the flash of your ESP as urlparse.py:

https://gist.github.com/SpotlightKid/88 ... 532b86cbeb

And then do:

Code: Select all

from urlparse import parse_qsl
url = 'http://example.com/get?power=10&time=0.7&long+name+with+spaces=value%20with%20%C3%BCnicode'
parse_qsl(url.split('?', 1)[1])
which results in:

Code: Select all

[('power', '10'), ('time', '0.7'), ('long name with spaces', 'value with \xfcnicode')]
You might want to pre-compile the module with mpy-cross or even put it into the firmware as a frozen module.

Post Reply