Pydantic model as body
Corefrom pydantic import BaseModel
from typing import Optional
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
@app.post("/items")
async def create_item(item: Item):
result = item.model_dump()
if item.tax:
result["price_with_tax"] = item.price + item.tax
return result
# JSON sent by client:
# { "name": "Foo", "price": 35.4, "tax": 3.2 }