We can read the incoming data stream from a
POST,PUTorPATCHrequest and parse it using middleware or built-in framework tools based on itsContent-Typeheader.Define a data schema using a Pydantic
BaseModeland pass it as a type-hinted parameter in route function.FastAPI reads, validates and converts the JSON body into that model automatically.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.post("/items/")
async def create_item(item: Item):
return item