From 6b5c60b2da592c86e2e18696906706d08082e180 Mon Sep 17 00:00:00 2001 From: dotdashnotdotsoftware <47453127+dotdashnotdotsoftware@users.noreply.github.com> Date: Sun, 22 Sep 2024 17:55:35 +0200 Subject: [PATCH] Add support for headers to urequest.urlopen Addition of an optional parameter which allows for the consumer to define headers to pass when making a HTTP request --- micropython/urllib.urequest/urllib/urequest.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/micropython/urllib.urequest/urllib/urequest.py b/micropython/urllib.urequest/urllib/urequest.py index f83cbaa94..a9622ea89 100644 --- a/micropython/urllib.urequest/urllib/urequest.py +++ b/micropython/urllib.urequest/urllib/urequest.py @@ -1,7 +1,7 @@ import socket -def urlopen(url, data=None, method="GET"): +def urlopen(url, data=None, method="GET", headers={}): if data is not None and method == "GET": method = "POST" try: @@ -40,6 +40,12 @@ def urlopen(url, data=None, method="GET"): s.write(host) s.write(b"\r\n") + for k in headers: + s.write(k) + s.write(b": ") + s.write(headers[k]) + s.write(b"\r\n") + if data: s.write(b"Content-Length: ") s.write(str(len(data)))