Query、Path、Body 终极混用
from typing import Optional import uvicorn from fastapi import Body, FastAPI, Path, Query from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str description: Optional[str] = None price: float tax: Optional[float] = None class User(BaseModel): username: str full_name: Optional[str] = None @app.put("/item_all/{item_id}") async def update_item( *, item_id: int = Path(default=..., description="路径参数", gt=0, lt=10), address: str = Query(default=None, description="查询参数", max_length=10), item: Item, user: User, importance: int = Body(default=..., description="请求体", ge=1, le=5) ): results = { "item_id": item_id, "address": address, "item": item, "user": user, "importance": importance } return results
正确传参的请求结果
查看 Swagger API 文档
Body() 中的 embed 参数
为什么要讲这个 embed 参数
当函数只有一个参数指定了 Pydantic Model 且没有其他 Body 参数时,传参的时候请求体可以不指定参数名
class Item(BaseModel): name: str description: Optional[str] = None price: float tax: Optional[float] = None @app.put("/items/{item_id}") async def update_item(item_id: int, item: Item): ...
期望得到的请求体
{
"name": "Foo",
"description": "The pretender",
"price": 42.0,
"tax": 3.2
}
默认并不需要指定 item 为字段名
那假设想指定 item 为请求体的字段名呢?就是通过 embed 参数达到目的了
实际代码
from typing import Optional from fastapi import Body, FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str description: Optional[str] = None price: float tax: Optional[float] = None @app.put("/items/{item_id}") async def update_item( *, item_id: int, # 将 embed 设置为 True item: Item = Body(..., embed=True)): results = {"item_id": item_id, "item": item} return results
期望得到的请求体
{ "item": { "name": "Foo", "description": "The pretender", "price": 42.0, "tax": 3.2 } }
正确传参的请求结果
还是不传 item 字段的请求结果
查看 Swagger API 文档