Request: params keyword argument for urequests
Currently, urequests does not support the params keyword of the original Python requests module.
However, there exists a better urequests implementation which does.
It would be great if that implementation would be adopted.
micropython-lib already contains everything you need to achieve the same:
import urequests
from urllib.parse import urlencode
def get(url, params=None, **kw):
if params:
url = url.rstrip('?') + '?' + urlencode(params, doseq=True)
return urequests.get(url, **kw)
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
res = get('http://httpbin.org/get', payload)
print(res.json())
Output:
{'url': 'http://httpbin.org/get?key2=value2&key2=value3&key1=value1',
'headers': {'Host': 'httpbin.org', 'Connection': 'close'},
'args': {'key2': ['value2', 'value3'], 'key1': 'value1'}, 'origin': 'XX.XXX.XXX.XXX'}
I'm just doing my first steps in esp32 programming with micropython, so please excuse if I miss something.
I wanted to use urequests to POST some data that uses application/x-www-form-urlencoded. So I need to urlencode my parameters. I can't seem to get it working, though. Installing urllib.parse via upip seems to work initially, but trying to actually import it in the python file results in
>>> from urllib.parse import urlencode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/lib/urllib/parse.py", line 30, in <module>
File "/lib/re.py", line 11, in <module>
AttributeError: 'NoneType' object has no attribute 'func'
Any other way to get urlencode working or do I need to implement it myself?
I created a cut-down version of urlencode for my mrequests module:
https://github.com/SpotlightKid/mrequests/blob/25eac2977f75699985ab735a257c2b8ba9a7a2aa/urlencode.py
Here's a usage example:
https://github.com/SpotlightKid/mrequests/blob/25eac2977f75699985ab735a257c2b8ba9a7a2aa/examples/formencode.py