Skip to content

Commit 49f58f4

Browse files
committed
Add simple support for redirects
Follow 3XX status redirects with a new request. Parse the 'location' header for an absolute url, absolute path, or relative path. Replace or amend the url from the original request and place a new request. Expand the requests_simpletest_cpython.py example with new requests utilizing the three supported types of redirects. Note: We're using the httpbingo.org domain for redirects until the following issue on httpbin.org is resolved: postmanlabs/httpbin#617 This has been tested on CPython and an ESP32-S2 based MagTag board.
1 parent 74b0a5a commit 49f58f4

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

adafruit_requests.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,28 @@ def request(
587587

588588
resp = Response(socket, self) # our response
589589
if "location" in resp.headers and 300 <= resp.status_code <= 399:
590-
raise NotImplementedError("Redirects not yet supported")
590+
# a naive handler for redirects
591+
redirect = resp.headers["location"]
592+
593+
if redirect.startswith("http"):
594+
# absolute URL
595+
url = redirect
596+
elif redirect[0] == "/":
597+
# relative URL, absolute path
598+
url = "/".join([proto, dummy, host, redirect[1:]])
599+
else:
600+
# relative URL, relative path
601+
path = path.rsplit("/", 1)[0]
602+
603+
while redirect.startswith("../"):
604+
path = path.rsplit("/", 1)[0]
605+
redirect = redirect.split("../", 1)[1]
606+
607+
url = "/".join([proto, dummy, host, path, redirect])
608+
609+
self._last_response = resp
610+
resp = self.request(method, url, data, json,
611+
headers, stream, timeout)
591612

592613
self._last_response = resp
593614
return resp

examples/requests_simpletest_cpython.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
1111
JSON_GET_URL = "http://httpbin.org/get"
1212
JSON_POST_URL = "http://httpbin.org/post"
13+
REDIRECT_URL = "http://httpbingo.org/redirect/1"
14+
RELATIVE_REDIRECT_URL = "http://httpbingo.org/relative-redirect/1"
15+
ABSOLUTE_REDIRECT_URL = "http://httpbingo.org/absolute-redirect/1"
1316

1417
print("Fetching text from %s" % TEXT_URL)
1518
response = http.get(TEXT_URL)
@@ -45,3 +48,26 @@
4548
# Parse out the 'json' key from json_resp dict.
4649
print("JSON Data received from server:", json_resp["json"])
4750
print("-" * 40)
51+
52+
print("Fetching JSON data from redirect url %s" % REDIRECT_URL)
53+
response = http.get(REDIRECT_URL)
54+
print("-" * 40)
55+
56+
print("JSON Response: ", response.json())
57+
print("-" * 40)
58+
59+
print("Fetching JSON data from relative redirect url %s" % RELATIVE_REDIRECT_URL)
60+
response = http.get(RELATIVE_REDIRECT_URL)
61+
print("-" * 40)
62+
63+
print("JSON Response: ", response.json())
64+
print("-" * 40)
65+
66+
print("Fetching JSON data from aboslute redirect url %s" % ABSOLUTE_REDIRECT_URL)
67+
response = http.get(ABSOLUTE_REDIRECT_URL)
68+
print("-" * 40)
69+
70+
print("JSON Response: ", response.json())
71+
print("-" * 40)
72+
73+
response.close()

0 commit comments

Comments
 (0)