Skip to content

Commit

Permalink
Add support for headers to urequest.urlopen
Browse files Browse the repository at this point in the history
Addition of an optional parameter which allows for the consumer to define headers to pass when making a HTTP request
  • Loading branch information
dotdashnotdotsoftware authored Sep 22, 2024
1 parent 27e4d73 commit 6b5c60b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion micropython/urllib.urequest/urllib/urequest.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)))
Expand Down

0 comments on commit 6b5c60b

Please sign in to comment.