Skip to content

Commit e7e8eff

Browse files
Fabian Schmittdpgeorge
Fabian Schmitt
authored andcommitted
urequests: Add Basic Authentication support.
Usage matches the shorthand version described in https://requests.readthedocs.io/en/latest/user/authentication/#basic-authentication
1 parent 106c28a commit e7e8eff

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

python-ecosys/urequests/urequests.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ def json(self):
3333
return ujson.loads(self.content)
3434

3535

36-
def request(method, url, data=None, json=None, headers={}, stream=None, parse_headers=True):
36+
def request(
37+
method, url, data=None, json=None, headers={}, stream=None, parse_headers=True, auth=None
38+
):
39+
if auth is not None:
40+
import ubinascii
41+
42+
username, password = auth
43+
formated = b"{}:{}".format(username, password)
44+
formated = str(ubinascii.b2a_base64(formated)[:-1], "ascii")
45+
headers["Authorization"] = "Basic {}".format(formated)
46+
3747
try:
3848
proto, dummy, host, path = url.split("/", 3)
3949
except ValueError:

0 commit comments

Comments
 (0)