FastAPI系列:请求体中的单一值Body
请求体中的单一值Body
from typing import Annotated
from fastapi import Body,FastAPI # Body()表示嵌入请求体中的单一值
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
is_offer: Union[bool, None] = None
class User(BaseModel):
username: str
full_name: str | None = None
@app.get("/items/{item_id}")
def update_item(item_id: int, item: Item, user: User, importance: Annotated[int, Body()]):
return {"item_id": item_id, "item": item, "user": user, "importance": importance}
"""请求体
{
"item":{
"name":"skt",
"price": 90.2,
"is_offer": true
},
"user":{
"username": "li",
"fullname": "lixianghe"
},
"importance": 1
}
"""
-------------------------------------------
个性签名:代码过万,键盘敲烂!!!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!