[SOLVED] urequests + redirects

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
dzairo
Posts: 3
Joined: Mon Jan 11, 2021 8:11 am

Re: [SOLVED] urequests + redirects

Post by dzairo » Mon Jan 18, 2021 7:42 pm

... no idea for urequests + redirects ???

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

Re: [SOLVED] urequests + redirects

Post by SpotlightKid » Mon Jan 18, 2021 10:59 pm

meebox wrote:
Tue Dec 01, 2020 11:51 am
mcauser wrote:
Fri May 31, 2019 8:42 am
It’s moved here:
https://github.com/pfalcon/micropython- ... /urequests
Can you show me how to use this library in MicroPython? I get error showing me the ussl module doesn't have the SSLContext method.Thanks.
Here, I fixed it for ya:

https://gist.github.com/SpotlightKid/86 ... 41dd269c44

Tested it on an ESP32 with pre-built firmware version esp32-idf3-20210118-unstable-v1.13-274-g49dd9ba1a.bin:

I renamed the module to 'prequests.py', because the current MicroPython firmware for the ESP32 already has an "urequest" module built in, but that does not support redirects and you can't overwrite it with a module file with the same name.

Code: Select all

$ mpy-cross prequests.py
$ rshell -p /dev/ttyUSB0 cp requests.mpy /pyboard
$ rshell -p /dev/ttyUSB0 repl
>>> import prequests
>>> # note NO trailing slash => triggers a redirect to 'https://chrisarndt.de/impressum/'
>>> r = prequests.get('https://chrisarndt.de/impressum')
>>> print(r.text[:100])
Hth, Chris

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

Re: [SOLVED] urequests + redirects

Post by SpotlightKid » Mon Jan 18, 2021 11:22 pm

mattyt wrote:
Wed Dec 02, 2020 1:17 am
Don't suppose anyone is keen to port - or write a subset - of httpx to MicroPython?
This would be unlikely to succeed. httpx has a current LOC of 7553 (LLOC: 3244) and depends on half a dozen other Python modules.

User avatar
mattyt
Posts: 329
Joined: Mon Jan 23, 2017 6:39 am

Re: [SOLVED] urequests + redirects

Post by mattyt » Mon Jan 18, 2021 11:32 pm

SpotlightKid wrote:
Mon Jan 18, 2021 11:22 pm
mattyt wrote:
Wed Dec 02, 2020 1:17 am
Don't suppose anyone is keen to port - or write a subset - of httpx to MicroPython?
This would be unlikely to succeed. httpx has a current LOC of 7553 (LLOC: 3244) and depends on half a dozen other Python modules.
Sure, but even a (preferably strict) subset of httpx - like using features already provided by prequests - would be useful.

It may be worth noting that the requests and httpx modules are in roughly the same ballpark of size.

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

Re: [SOLVED] urequests + redirects

Post by SpotlightKid » Mon Jan 18, 2021 11:41 pm

Yeah, and neither would fit on an MicroPython board without serious reworking ("urequest.py" has not much in common with "requests" except the name and the module-level function names). At which point it is easier to (re-)implement the features you need from them.

Feel free to prove me wrong by porting it. ;)

dzairo
Posts: 3
Joined: Mon Jan 11, 2021 8:11 am

Re: [SOLVED] urequests + redirects

Post by dzairo » Tue Jan 19, 2021 4:26 pm

Thanks, I try it tomorrow..
I want send data to google spreadsheet.. .

dzairo
Posts: 3
Joined: Mon Jan 11, 2021 8:11 am

Re: [SOLVED] urequests + redirects

Post by dzairo » Tue Jan 19, 2021 6:47 pm

Super ... yes .. working .. perfect .. super .. thanks ..

Send data to Google SpreadSheet and return back answer from server .

thanks ..

meebox
Posts: 10
Joined: Sat Feb 01, 2020 4:41 am

Re: [SOLVED] urequests + redirects

Post by meebox » Wed Jan 20, 2021 7:52 am

I'll try it.Thanks.
SpotlightKid wrote:
Mon Jan 18, 2021 10:59 pm
meebox wrote:
Tue Dec 01, 2020 11:51 am
mcauser wrote:
Fri May 31, 2019 8:42 am
It’s moved here:
https://github.com/pfalcon/micropython- ... /urequests
Can you show me how to use this library in MicroPython? I get error showing me the ussl module doesn't have the SSLContext method.Thanks.
Here, I fixed it for ya:

https://gist.github.com/SpotlightKid/86 ... 41dd269c44

Tested it on an ESP32 with pre-built firmware version esp32-idf3-20210118-unstable-v1.13-274-g49dd9ba1a.bin:

I renamed the module to 'prequests.py', because the current MicroPython firmware for the ESP32 already has an "urequest" module built in, but that does not support redirects and you can't overwrite it with a module file with the same name.

Code: Select all

$ mpy-cross prequests.py
$ rshell -p /dev/ttyUSB0 cp requests.mpy /pyboard
$ rshell -p /dev/ttyUSB0 repl
>>> import prequests
>>> # note NO trailing slash => triggers a redirect to 'https://chrisarndt.de/impressum/'
>>> r = prequests.get('https://chrisarndt.de/impressum')
>>> print(r.text[:100])
Hth, Chris

Post Reply