Skip to content

Commit 5b1a83a

Browse files
committed
HTTP: Add POST function
This is used by OIDC.
1 parent 6b65143 commit 5b1a83a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

not_my_board/_http.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,37 @@ class ProtocolError(Exception):
1414

1515

1616
async def get_json(url):
17+
return await _request_json("GET", url)
18+
19+
20+
async def post_form(url, params):
21+
content_type="application/x-www-form-urlencoded"
22+
body = urllib.parse.urlencode(params).encode()
23+
return await _request_json("POST", url, content_type, body)
24+
25+
26+
async def _request_json(method, url, content_type=None, body=None):
1727
url = urllib.parse.urlsplit(url)
1828
headers = [
1929
("Host", url.netloc),
2030
("User-Agent", h11.PRODUCT_ID),
2131
("Accept", "application/json"),
2232
("Connection", "close"),
2333
]
34+
if body is not None:
35+
headers += [
36+
("Content-Type", content_type),
37+
("Content-Length", str(len(body))),
38+
]
2439

2540
conn = h11.Connection(our_role=h11.CLIENT)
41+
2642
to_send = conn.send(
27-
h11.Request(method="GET", target=url.path or "/", headers=headers)
43+
h11.Request(method=method, target=url.path or "/", headers=headers)
2844
)
45+
if body is not None:
46+
to_send += conn.send(h11.Data(body))
47+
to_send += conn.send(h11.EndOfMessage())
2948

3049
if url.scheme == "https":
3150
default_port = 443
@@ -40,7 +59,6 @@ async def get_json(url):
4059

4160
async with util.connect(url.hostname, port, ssl=ssl) as (reader, writer):
4261
writer.write(to_send)
43-
writer.write(conn.send(h11.EndOfMessage()))
4462
await writer.drain()
4563

4664
async def receive_all():

0 commit comments

Comments
 (0)