Make query values in basic request flatten when provided as single values

This commit is contained in:
2025-07-16 04:52:40 +03:00
parent b57c5d9639
commit 7546055574
2 changed files with 11 additions and 5 deletions

View File

@@ -14,6 +14,5 @@ async def index(req: BasicRequest) -> BasicResponse:
@app.get("/user/")
async def get_user(req: BasicRequest) -> BasicResponse:
text = f"User ID: ".encode("utf-8")
return BasicResponse(200, {}, b'Hello, User!')
print(req.query)
return BasicResponse(200, {}, f'Hello, User {req.query["id"]}!'.encode())

View File

@@ -32,7 +32,7 @@ class BasicRequest:
method: str
path: str
headers: dict[str, str]
query: dict[str, list[str]]
query: dict[str, list[str] | str]
body: dict[str, Any]
@classmethod
@@ -43,7 +43,14 @@ class BasicRequest:
for key, value in scope.get('headers', []):
headers[key.decode('latin1')] = value.decode('latin1')
query = urllib.parse.parse_qs(scope['query_string'].decode('latin1'))
qs = scope['query_string'].decode('latin1')
print(qs)
query_raw = urllib.parse.parse_qs(qs)
query = {}
for k, v in query_raw.items():
if len(v) == 1:
v = v[0]
query[k] = v
body = scope['body']