[SOLVED] urequests + redirects

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
mcauser
Posts: 453
Joined: Mon Jun 15, 2015 8:03 am

[SOLVED] urequests + redirects

Post by mcauser » Mon Sep 24, 2018 12:38 am

I've been experimenting with integrating with Google Sheets and hit the exception:
NotImplementedError("Redirects not yet supported").
To get data out of Google, you need to make a GET request to their /exec page, which performs a Location: redirect to the target page.

The /exec page always returns a redirect. Would it be possible to catch the exception, extract the Location header and make a 2nd urequest?
Maybe make a HEAD request to get the redirect, then make a GET request?
Last edited by mcauser on Wed Sep 26, 2018 5:35 am, edited 1 time in total.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: urequests + redirects

Post by jickster » Mon Sep 24, 2018 12:54 am

What module and function throws this?


Sent from my iPhone using Tapatalk Pro

User avatar
mcauser
Posts: 453
Joined: Mon Jun 15, 2015 8:03 am

Re: urequests + redirects

Post by mcauser » Mon Sep 24, 2018 1:16 am

Make a get request using urequests.py from micropython-lib

Copied this file to my board:
https://github.com/micropython/micropyt ... equests.py

Code: Select all

import urequests
r = urequests.get('https://script.google.com/macros/s/(token)/exec')
which redirects to: https://script.googleusercontent.com/ma ... &lib=(lib)
then throws NotImplementedError: Redirects not yet supported

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

Re: urequests + redirects

Post by SpotlightKid » Mon Sep 24, 2018 2:21 pm

The version of urequests.py in pfalcon's micropython-lib repo has support for redirects:

https://github.com/pfalcon/micropython- ... equests.py

User avatar
mcauser
Posts: 453
Joined: Mon Jun 15, 2015 8:03 am

Re: urequests + redirects

Post by mcauser » Wed Sep 26, 2018 5:35 am

It works! Thanks @pfalcon + @SpotlightKid

I'm able to get/post data to/from Google sheets and display on a Nokia 5110 + ESP8266.
I'll polish the code and share a tutorial soon.

JoM
Posts: 2
Joined: Fri May 31, 2019 8:21 am

Re: [SOLVED] urequests + redirects

Post by JoM » Fri May 31, 2019 8:29 am

mcauser, This sounds very promising.
Could you publish the code please, even if it is not polished.
Unfortunately the link given by SpotlightKid is broken.

User avatar
mcauser
Posts: 453
Joined: Mon Jun 15, 2015 8:03 am

Re: [SOLVED] urequests + redirects

Post by mcauser » Fri May 31, 2019 8:42 am


Jeffy
Posts: 1
Joined: Tue Jul 07, 2020 3:17 pm
Location: Thoothukudi

Re: [SOLVED] urequests + redirects

Post by Jeffy » Tue Jul 07, 2020 3:46 pm

Help Please
I am trying to receive the response back from my google App-Script, since it is redirected, I can not proceed in my code, because request response is used to decide function of the hardware. I am very new to micro Python and I am using ESP8266 and uPyCraft installed in a windows 10 machine. I have attached the code used and the output I got for reference. I came to know that above post had solved the issues, but I did not know how to replace the original urequest module with the pycopy-lib/urequests/ downloaded from https://github.com/pfalcon/pycopy-lib/t ... /urequests as zip file. I searched and tried a lot for more than three days, but i am unable to do things up. So kindly provide a detailed instructions. Looking Forward, Thanks in advance. ;)

Code: Select all

import network, time, urequests
print("-----Import OK-----")

ssid = 'WiFi'
password = 'embedded*7'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
  pass
print('SSID   : ', ssid)
print('PASS   : ', password)
print('IP ADD :', station.ifconfig()[0])
time.sleep(1)

url = 'https://script.google.com/macros/s/AKfycbzeBzs4xpdj5pMRpcyERt6xyLOBfoxjgktumoDxKoaQjXXXXXXXX/exec?'
url = url + 'station_id=tuticorin'
try:
  global request
  request = urequests.get(url=url)
  print(request.text)
except Exception as e:
  print("EXCEPTION CAPTURED:\n",e)
OUTPUT:

Code: Select all

>>> 

Ready to download this file,please wait!
.......
download ok
exec(open('./socket_http_post.py').read(),globals())
-----Import OK-----
SSID   :  WiFi
PASS   :  embedded*7
IP ADD : 192.168.1.106
EXCEPTION CAPTURED:
 Redirects not yet supported
>>> 

Post Reply