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
}
"""
posted @ 2024-02-28 17:24  我在路上回头看  阅读(127)  评论(0编辑  收藏  举报