
FastAPI
Learn the fundamentals of multiple bodies in FastAPI.
You can combine multiple request body models in a single endpoint using type unions or embedding objects. This allows accepting complex request structures with multiple related data models.
In this section, you'll understand:
from fastapi import FastAPI, Body
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class Order(BaseModel):
order_id: str
items: list[Item]
@app.post("/process/")
async def process(
order: Order = Body(...),
priority: int = Body(..., embed=True)
):
return {"order": order, "priority": priority}# Use Body() with embed=True to wrap single values
@app.post("/items/")
async def create_item(
item: Item,
quantity: int = Body(..., embed=True)
):
# Request: {"item": {...}, "quantity": 5}
return {"item": item, "quantity": quantity}Multiple bodies are used for:
Ready to explore more? Check out the advanced section for production patterns and edge cases.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward