← Back to FastAPI, page 4

fastapi-0061 · Easy fresher

How do multiple body parameters work in FastAPI, and what do Body() and embed=True change?

With one Pydantic model parameter, FastAPI expects the JSON body to be that model directly. With several model parameters it expects an object keyed by parameter name. Body(embed=True) forces the keyed form for a single model, and Body() lets singular values such as int or str come from the body instead of the query string. Embedding makes payloads extensible without breaking clients.

Full answer

Rules FastAPI follows:

  1. One body model: def create(student: Student) expects {"name": "...", ...}.
  2. Several body models: def enrol(student: Student, course: Course) expects {"student": {...}, "course": {...}}.
  3. A scalar annotated with Body() is taken from the body, not the query: priority: Annotated[int, Body()] adds a key priority next to the models.
  4. student: Annotated[Student, Body(embed=True)] expects {"student": {...}} even with a single model.
  5. Parameters that are plain scalars without Body() are query parameters; parameters with the same name as a path variable are path parameters.

Why embed: starting with {"student": {...}} leaves room to add sibling keys later ("notify": true) without changing the shape clients send. Starting with the bare model and later adding a second body parameter changes the format for all clients, which is a breaking change.

Related tools:

  • Body(examples=[...]) documents payload examples in /docs.
  • Body(media_type="application/json") is the default; for form data use Form(), for files File() and UploadFile. A request body cannot mix JSON Body parameters with Form fields, because HTTP bodies carry one encoding.
  • GET requests technically allow bodies but many clients, proxies and caches do not handle them, so use POST (or query parameters) for searches.

The generated OpenAPI shows a combined schema for multiple body parameters. In tests, send the same keyed structure with client.post(url, json={...}).

python
from typing import Annotated

from fastapi import Body, FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel

app = FastAPI()


class Student(BaseModel):
    name: str


class Course(BaseModel):
    title: str


@app.post("/single")
def single(student: Student):
    return {"student": student.name}


@app.post("/embedded")
def embedded(student: Annotated[Student, Body(embed=True)]):
    return {"student": student.name}


@app.post("/enrol")
def enrol(student: Student, course: Course, priority: Annotated[int, Body(ge=1, le=5)] = 3):
    return {"student": student.name, "course": course.title, "priority": priority}


c = TestClient(app)
print(c.post("/single", json={"name": "Asha"}).json())
print(c.post("/embedded", json={"student": {"name": "Asha"}}).json())
print(c.post("/embedded", json={"name": "Asha"}).status_code)
print(c.post("/enrol", json={"student": {"name": "Asha"}, "course": {"title": "DBMS"}, "priority": 1}).json())
print(c.post("/enrol", json={"student": {"name": "Asha"}, "course": {"title": "DBMS"}}).json())
print(c.post("/enrol", json={"name": "Asha"}).json()["detail"][0]["loc"])

Standalone script. Run it with `python file.py`; it calls the app through TestClient, so no server is needed. Output (ran twice with identical results; Python 3.13.5, FastAPI 0.165.1, Pydantic 2.14.4, Starlette 1.10.0, SQLAlchemy 2.1.9, httpx 0.29.0): {'student': 'Asha'} {'student': 'Asha'} 422 {'student': 'Asha', 'course': 'DBMS', 'priority': 1} {'student': 'Asha', 'course': 'DBMS', 'priority': 3} ['body', 'student']

Key points

  • One model: bare object; several models: keyed by parameter name
  • Body(embed=True) forces the keyed form
  • Body() makes a scalar come from the JSON body
  • Embedding keeps payloads extensible
  • Do not mix JSON body and Form fields

Common mistakes

  • Sending a bare object when several body parameters expect keys
  • Forgetting Body() and sending a scalar in the query string
  • Mixing Form fields with JSON body parameters
  • Changing from one to several body parameters and breaking clients
  • Using GET with a body

Follow-ups

  • How would you add a second body parameter without breaking existing clients?
  • How do you document request examples?
  • Can a request body mix JSON and files?

Tips

  • State the three shapes
  • Explain why embed helps
  • Mention Form vs JSON