Header support in urllib or requests

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
hansamann
Posts: 7
Joined: Thu Jun 09, 2016 1:37 pm

Header support in urllib or requests

Post by hansamann » Thu Jun 09, 2016 2:38 pm

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.

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

Re: Header support in urllib or requests

Post by deshipu » Thu Jun 09, 2016 3:01 pm

You can use urequests instead, which has support for headers.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Header support in urllib or requests

Post by pfalcon » Thu Jun 09, 2016 3:03 pm

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/

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

Re: Header support in urllib or requests

Post by deshipu » Thu Jun 09, 2016 3:04 pm

Also, it seems that urllib.urequest does support headers:

Code: Select all

response = urllib.urequest.urlopen(endpoint, data=urlencoded, headers=headers, method="POST")

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Header support in urllib or requests

Post by pfalcon » Thu Jun 09, 2016 10:47 pm

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/

Post Reply