Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def _is_body(anno): return issubclass(anno, (dict,ns)) or _annotations(anno)
# %% ../nbs/api/00_core.ipynb
def _formitem(form, k):
"Return single item `k` from `form` if len 1, otherwise return list"
if isinstance(form, dict): return form[k]
if isinstance(form, dict): return form.get(k)
o = form.getlist(k)
return o[0] if len(o) == 1 else o if o else None

Expand Down
84 changes: 80 additions & 4 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
{
"data": {
"text/plain": [
"datetime.datetime(2025, 3, 16, 14, 0)"
"datetime.datetime(2025, 3, 28, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -487,7 +487,7 @@
"#| export\n",
"def _formitem(form, k):\n",
" \"Return single item `k` from `form` if len 1, otherwise return list\"\n",
" if isinstance(form, dict): return form[k]\n",
" if isinstance(form, dict): return form.get(k)\n",
" o = form.getlist(k)\n",
" return o[0] if len(o) == 1 else o if o else None"
]
Expand Down Expand Up @@ -713,6 +713,82 @@
"print(response.text)"
]
},
{
"cell_type": "markdown",
"id": "34066c89",
"metadata": {},
"source": [
"**Missing Request Params**"
]
},
{
"cell_type": "markdown",
"id": "06259883",
"metadata": {},
"source": [
"If a request param has a default value (e.g. `a:str=''`), the request is valid even if the user doesn't include the param in their request."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c369a89c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[<starlette.requests.Request object>, <starlette.applications.Starlette object>, '']\n"
]
}
],
"source": [
"def g(req, this:Starlette, a:str=''): ...\n",
"\n",
"async def f(req):\n",
" a = await _wrap_req(req, _params(g))\n",
" return Response(str(a))\n",
"\n",
"client = TestClient(Starlette(routes=[Route('/', f, methods=['POST'])]))\n",
"response = client.post('/', json={}) # no param in request\n",
"print(response.text)"
]
},
{
"cell_type": "markdown",
"id": "35b68f4f",
"metadata": {},
"source": [
"If we remove the default value and re-run the request, we should get the following error `Missing required field: a`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b33b43a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Missing required field: a\n"
]
}
],
"source": [
"def g(req, this:Starlette, a:str): ...\n",
"\n",
"async def f(req):\n",
" a = await _wrap_req(req, _params(g))\n",
" return Response(str(a))\n",
"\n",
"client = TestClient(Starlette(routes=[Route('/', f, methods=['POST'])]))\n",
"response = client.post('/', json={}) # no param in request\n",
"print(response.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -3342,9 +3418,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"display_name": "python",
"language": "python",
"name": "python3"
"name": "python"
}
},
"nbformat": 4,
Expand Down