Skip to content

Commit 25964b8

Browse files
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
1 parent 567540d commit 25964b8

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

micropython/urllib.urequest/urllib/urequest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import socket
22

33

4-
def urlopen(url, data=None, method="GET"):
4+
def urlopen(url, data=None, method="GET", headers={}):
55
if data is not None and method == "GET":
66
method = "POST"
77
try:
@@ -40,6 +40,12 @@ def urlopen(url, data=None, method="GET"):
4040
s.write(host)
4141
s.write(b"\r\n")
4242

43+
for k in headers:
44+
s.write(k)
45+
s.write(b": ")
46+
s.write(headers[k])
47+
s.write(b"\r\n")
48+
4349
if data:
4450
s.write(b"Content-Length: ")
4551
s.write(str(len(data)))

0 commit comments

Comments
 (0)