Looking at the urllib.urequest code, it seems there is no header support at all. That's the reason why my current request is failing. Is there a chance to get header support implemented soon?
import urllib.urequest
endpoint = 'https://api.yaas.io/hybris/oauth2/b1/token'
urlencoded = 'grant_type=client_credentials&scope=&client_id=xxx&client_secret=xxx'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = urllib.urequest.urlopen(endpoint, data=urlencoded, method="POST")
json = response.read()
//FAILS - no Content-Type specified and the API does not seem to support it right now.
Header support in urllib or requests
Re: Header support in urllib or requests
You can use urequests instead, which has support for headers.
Re: Header support in urllib or requests
Unfortunately, it doesn't. Let's see if this gets reported on github, so maintainers didn't forget to fix it .
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Re: Header support in urllib or requests
Also, it seems that urllib.urequest does support headers:
Code: Select all
response = urllib.urequest.urlopen(endpoint, data=urlencoded, headers=headers, method="POST")
Re: Header support in urllib or requests
Arbitrary headers support has been added to urequests. Please test it and report results.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/