@raphael_tillman
In FastAPI, you can validate request body by using Pydantic models. Here's an example of how to do it:
1 2 3 4 5 |
from pydantic import BaseModel class Item(BaseModel): name: str price: float |
1 2 3 4 5 6 7 8 9 10 11 12 |
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items/") def create_item(item: Item): return {"name": item.name, "price": item.price} |
By using Pydantic models in this way, you can easily validate request bodies in FastAPI routes.