Skip to content

Commit 49e173d

Browse files
committed
fixes #439
1 parent 674cd97 commit 49e173d

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

fasthtml/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ async def _from_body(req, p):
167167
d = _annotations(anno)
168168
if req.headers.get('content-type', None)=='application/json': data = await req.json()
169169
else: data = form2dict(await parse_form(req))
170+
if req.query_params: data = {**data, **dict(req.query_params)}
170171
cargs = {k: _form_arg(k, v, d) for k, v in data.items() if not d or k in d}
171172
return anno(**cargs)
172173

nbs/api/00_core.ipynb

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
{
130130
"data": {
131131
"text/plain": [
132-
"datetime.datetime(2024, 9, 17, 14, 0)"
132+
"datetime.datetime(2024, 9, 19, 14, 0)"
133133
]
134134
},
135135
"execution_count": null,
@@ -588,7 +588,7 @@
588588
{
589589
"cell_type": "code",
590590
"execution_count": null,
591-
"id": "0b6ac588",
591+
"id": "33d3bc16",
592592
"metadata": {},
593593
"outputs": [],
594594
"source": [
@@ -599,6 +599,7 @@
599599
" d = _annotations(anno)\n",
600600
" if req.headers.get('content-type', None)=='application/json': data = await req.json()\n",
601601
" else: data = form2dict(await parse_form(req))\n",
602+
" if req.query_params: data = {**data, **dict(req.query_params)}\n",
602603
" cargs = {k: _form_arg(k, v, d) for k, v in data.items() if not d or k in d}\n",
603604
" return anno(**cargs)"
604605
]
@@ -734,6 +735,34 @@
734735
"print(response.text)"
735736
]
736737
},
738+
{
739+
"cell_type": "code",
740+
"execution_count": null,
741+
"id": "a3ded5ec",
742+
"metadata": {},
743+
"outputs": [
744+
{
745+
"name": "stdout",
746+
"output_type": "stream",
747+
"text": [
748+
"[<starlette.requests.Request object>, <starlette.applications.Starlette object>, '1', HttpHeader(k='value1', v='value3')]\n"
749+
]
750+
}
751+
],
752+
"source": [
753+
"def g(req, this:Starlette, a:str, b:HttpHeader): ...\n",
754+
"\n",
755+
"async def f(req):\n",
756+
" a = await _wrap_req(req, _sig(g).parameters)\n",
757+
" return Response(str(a))\n",
758+
"\n",
759+
"app = Starlette(routes=[Route('/', f, methods=['POST'])])\n",
760+
"client = TestClient(app)\n",
761+
"\n",
762+
"response = client.post('/?a=1', data=d)\n",
763+
"print(response.text)"
764+
]
765+
},
737766
{
738767
"cell_type": "code",
739768
"execution_count": null,
@@ -2084,7 +2113,7 @@
20842113
" a:int|None; b:str\n",
20852114
" def __init__(self, a, b='foo'): store_attr()\n",
20862115
"\n",
2087-
"@app.post(\"/bodie2/\")\n",
2116+
"@rt(\"/bodie2/\", methods=['get','post'])\n",
20882117
"def bodie(d:Bodie2): return f\"a: {d.a}; b: {d.b}\""
20892118
]
20902119
},
@@ -2101,7 +2130,24 @@
21012130
{
21022131
"cell_type": "code",
21032132
"execution_count": null,
2104-
"id": "6c1fbc4b",
2133+
"id": "48f0a45e",
2134+
"metadata": {},
2135+
"outputs": [],
2136+
"source": [
2137+
"d = dict(a=1, b='foo')\n",
2138+
"\n",
2139+
"test_r(cli, '/bodie/me', '{\"a\":1,\"b\":\"foo\",\"nm\":\"me\"}', 'post', data=dict(a=1, b='foo', nm='me'))\n",
2140+
"test_r(cli, '/bodied/', '{\"a\":\"1\",\"b\":\"foo\"}', 'post', data=d)\n",
2141+
"test_r(cli, '/bodie2/', 'a: 1; b: foo', 'post', data={'a':1})\n",
2142+
"test_r(cli, '/bodie2/?a=1&b=foo&nm=me', 'a: 1; b: foo')\n",
2143+
"test_r(cli, '/bodient/', '{\"a\":\"1\",\"b\":\"foo\"}', 'post', data=d)\n",
2144+
"test_r(cli, '/bodietd/', '{\"a\":1,\"b\":\"foo\"}', 'post', data=d)"
2145+
]
2146+
},
2147+
{
2148+
"cell_type": "code",
2149+
"execution_count": null,
2150+
"id": "73363a37",
21052151
"metadata": {},
21062152
"outputs": [],
21072153
"source": [
@@ -2117,7 +2163,7 @@
21172163
{
21182164
"cell_type": "code",
21192165
"execution_count": null,
2120-
"id": "51cd4643",
2166+
"id": "0aeaac36",
21212167
"metadata": {},
21222168
"outputs": [],
21232169
"source": [
@@ -2129,22 +2175,6 @@
21292175
"test_eq(response, '{\"b\": \"Lorem\", \"a\": 15}')"
21302176
]
21312177
},
2132-
{
2133-
"cell_type": "code",
2134-
"execution_count": null,
2135-
"id": "48f0a45e",
2136-
"metadata": {},
2137-
"outputs": [],
2138-
"source": [
2139-
"d = dict(a=1, b='foo')\n",
2140-
"\n",
2141-
"test_r(cli, '/bodie/me', '{\"a\":1,\"b\":\"foo\",\"nm\":\"me\"}', 'post', data=dict(a=1, b='foo', nm='me'))\n",
2142-
"test_r(cli, '/bodied/', '{\"a\":\"1\",\"b\":\"foo\"}', 'post', data=d)\n",
2143-
"test_r(cli, '/bodie2/', 'a: 1; b: foo', 'post', data={'a':1})\n",
2144-
"test_r(cli, '/bodient/', '{\"a\":\"1\",\"b\":\"foo\"}', 'post', data=d)\n",
2145-
"test_r(cli, '/bodietd/', '{\"a\":1,\"b\":\"foo\"}', 'post', data=d)"
2146-
]
2147-
},
21482178
{
21492179
"cell_type": "code",
21502180
"execution_count": null,
@@ -2155,13 +2185,13 @@
21552185
"name": "stdout",
21562186
"output_type": "stream",
21572187
"text": [
2158-
"Set to 2024-09-15 07:09:53.847227\n"
2188+
"Set to 2024-09-19 11:49:19.536169\n"
21592189
]
21602190
},
21612191
{
21622192
"data": {
21632193
"text/plain": [
2164-
"'Session time: 2024-09-15 07:09:53.847227'"
2194+
"'Session time: 2024-09-19 11:49:19.536169'"
21652195
]
21662196
},
21672197
"execution_count": null,
@@ -2392,7 +2422,7 @@
23922422
{
23932423
"data": {
23942424
"text/plain": [
2395-
"'Cookie was set at time 07:09:55.075607'"
2425+
"'Cookie was set at time 11:49:19.827380'"
23962426
]
23972427
},
23982428
"execution_count": null,

0 commit comments

Comments
 (0)