声明的参数不是路径参数时,路径操作函数会把该参数自动解释为查询参数。
from fastapi import FastAPI
app = FastAPI()
fake_items_db = [{"item_name": "foo"}, {"item_name": "bar"}, {"item_name": "baz"}]
@app.get("/items/")
async def read_item(skip: int = 0, limit: int = 10):
return fake_items_db[skip : skip + limit]
查询字符串是键值对的集合,这些键值对位于URL的 ?
之后,以&
分隔。
上面运行后访问的URL为: http:/127.0.0.1:8000/items/?skip=0&limit=10
访问结果为:
[
{
"item_name":"Foo"
},
{
"item_name":"Bar"
},
{
"item_name":"Baz"
}
]
默认值
上面代码中 skip=0
和 limit=10
设定为默认值。
如果访问: http:127.0.0.1:8000/items/?skip=20
,skip=20
为在URL中设定的值,这时limit=10
,表示默认值为10.
可选参数
from fastapi import FastAPI
from typing import Optional
app = FastAPI()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Optional[str] = None):
if q:
return {"item_id": item_id, "q": q}
return {"item_id": item_id}
本例中,查询参数 q 是可选的,默认值为None。
查询参数类型转换
参数还可以声明为bool类型,FastAPI会自动转换参数类型:
from typing import Optional
from fastapi import FastAPI
app = FastAIP()
@app.get("/items/{item_id}")
async def read_item(item_id: str, q: Optional[str] = None, short: bool = False):
item = {"item_id": item_id}
if q:
item.update({"q": q})
if not short:
item.update(
{"description": "This is an amazing item that has a long description"}
)
return item
访问URL http://127.0.0.1:8000/items/foo?short=1
的结果如下:
{
"item_id":"foo"
}
访问URL http://127.0.0.1:8000/items/foo?short=True
的结果如下:
{
"item_id":"foo"
}
访问URL http://127.0.0.1:8000/items/foo?short=true
的结果如下:
{
"item_id":"foo"
}
访问URL http://127.0.0.1:8000/items/foo?short=false
的结果如下:
{
"item_id":"foo","description":"This is an amazing item that has a long description"
}
访问URL http://127.0.0.1:8000/items/foo?short=False
的结果如下:
{
"item_id":"foo","description":"This is an amazing item that has a long description"
}
访问URL http://127.0.0.1:8000/items/foo
的结果如下:
{
"item_id":"foo","description":"This is an amazing item that has a long description"
}
多个路径参数和查询参数
FastAPI可以识别同时声明的多个路径参数和查询参数,而且声明查询参数的顺序并不重要。
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(user_id: int, item_id: str, q: Optional[str] = None, short: bool = False):
item = {"item_id": item_id, "owner_id": user_id}
if q:
item.update({"q": q})
if not short:
item.update({"description": "This is an amazing item that has a long description"})
return item
必选查询参数
如果要把查询参数设置为必选,就不要声明默认值:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
item = {"item_id": item_id, "needy": needy}
return item
这里的查询参数 needy
的类型为 str
的必选查询参数。
访问URL http://127.0.0.1:8000/items/foo-item
时报错,报错信息如下:
{
"detail": [
{
"loc": [
"query",
"needy"
],
"msg": "field required",
"type": "value_error.missing"
}
]
}
访问URL http://127.0.0.1:8000/items/foo-item?needy=soomneedy
的结果如下:
{
"item_id": "foo-item",
"needy": "sooooneedy"
}
当然,把一些参数定义为必选,另一些参数设置为默认值,再把其他参数定义为可选,这些操作都是可以的。
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(
item_id: str, needy: str, skip: int = 0, limit: Optional[int] = None
):
item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
return item
上面代码中参数分别是:
needy
为必选参数skip
为默认值参数,默认值为0limit
为可选参数
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!