How to Use Request Body in FastAPI: Simple Guide
In FastAPI, you use the
request body by defining a Pydantic model that describes the expected data structure and then declaring it as a function parameter in your path operation. FastAPI automatically reads the JSON body, validates it, and converts it into the model instance for you to use inside your function.Syntax
To use the request body in FastAPI, define a Pydantic model class that describes the data fields you expect. Then, add a parameter of that model type to your path operation function. FastAPI will parse and validate the incoming JSON body into this model.
- Pydantic model: Defines the shape and types of the data.
- Function parameter: Declares the model type to receive the request body.
- Automatic parsing: FastAPI converts JSON body to the model instance.
python
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float is_offer: bool = None @app.post("/items/") async def create_item(item: Item): return item
Example
This example shows a FastAPI app that accepts a JSON request body with name, price, and optional is_offer fields. The data is validated and returned as JSON.
python
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float is_offer: bool = None @app.post("/items/") async def create_item(item: Item): return {"item_name": item.name, "item_price": item.price, "offer": item.is_offer} # To test, send POST request to /items/ with JSON body: # {"name": "Book", "price": 12.5, "is_offer": true}
Output
POST /items/ with JSON {"name": "Book", "price": 12.5, "is_offer": true}
Response:
{"item_name": "Book", "item_price": 12.5, "offer": true}
Common Pitfalls
Common mistakes when using request body in FastAPI include:
- Not using a Pydantic model and trying to read raw JSON manually.
- Forgetting to declare the parameter type as the model, so FastAPI does not parse the body.
- Sending invalid JSON or missing required fields causes validation errors.
- Using query parameters or path parameters instead of the request body for complex data.
Always use Pydantic models for clear, validated request bodies.
python
from fastapi import FastAPI app = FastAPI() # Wrong: no model, no parsing @app.post("/wrong/") async def wrong_endpoint(item): return item # Right: use Pydantic model from pydantic import BaseModel class Item(BaseModel): name: str price: float @app.post("/right/") async def right_endpoint(item: Item): return item
Quick Reference
Tips for using request body in FastAPI:
- Define a Pydantic model for the expected data.
- Use the model as a function parameter to receive the body.
- FastAPI handles JSON parsing and validation automatically.
- Optional fields can have default values or be set to
None. - Validation errors return clear HTTP 422 responses.
Key Takeaways
Use Pydantic models to define and validate request body data in FastAPI.
Declare the model as a parameter in your path operation function to receive the body.
FastAPI automatically parses JSON request bodies into model instances.
Validation errors are handled automatically with clear error messages.
Avoid manual JSON parsing; rely on FastAPI and Pydantic for clean code.