From 96bd01ec047923c15bf936d2f77043968745542d Mon Sep 17 00:00:00 2001 From: Adam Lewis Date: Sun, 22 Sep 2024 17:55:35 +0200 Subject: [PATCH] urllib.urequest: Add support for headers to urequest.urlopen. This is an extension to CPython, similar to the `method` argument. Signed-off-by: Adam Lewis --- micropython/urllib.urequest/manifest.py | 2 +- micropython/urllib.urequest/urllib/urequest.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/micropython/urllib.urequest/manifest.py b/micropython/urllib.urequest/manifest.py index 2790208a7..033022711 100644 --- a/micropython/urllib.urequest/manifest.py +++ b/micropython/urllib.urequest/manifest.py @@ -1,4 +1,4 @@ -metadata(version="0.7.0") +metadata(version="0.8.0") # Originally written by Paul Sokolovsky. 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)))