Body - Multiple Parameters
Part of: FastAPI Basics
Master all aspects of FastAPI's Body - Multiple Parameters following the complete official tutorial. Learn to mix parameters, use multiple body params, singular values, and more.
What You'll Learn
- Mix path, query, and body parameters in one endpoint
- Declare multiple body parameters using Pydantic models
- Add singular values to request body using Body()
- Combine multiple body parameters with query parameters
- Embed single body parameters in JSON objects
Theory and Concepts
Body - Multiple Parameters
🎯 What You'll Learn
This lesson covers the complete official FastAPI "Body - Multiple Parameters" tutorial. You'll master all the flexible ways FastAPI handles body parameters, from basic multiple models to advanced parameter mixing.
By the end of this lesson, you'll understand how to:
- Mix path, query, and body parameters in one endpoint
- Declare multiple body parameters using Pydantic models
- Add singular values to request body using Body()
- Combine multiple body parameters with query parameters
- Embed single body parameters in JSON objects
📚 1. Mix Path, Query and Body Parameters
FastAPI allows you to freely mix different parameter types. You can make body parameters optional by setting default to None:
[Code Example]
Request examples:
- PUT /items/42/basic (no query, no body)
- PUT /items/42/basic?q=search (with query, no body)
- PUT /items/42/basic with JSON body (no query, with body)
🎯 2. Multiple Body Parameters
When you declare multiple Pydantic models as parameters, FastAPI expects a JSON body with keys for each model:
[Code Example]
Expected request body:
[Code Example]
FastAPI automatically converts the request so item receives its content and user receives its content.
💡 3. Singular Values in Body
Use Body() to add singular values (not Pydantic models) to the request body:
[Code Example]
Expected request body:
[Code Example]
Without Body(), FastAPI would treat importance as a query parameter.
🔄 4. Multiple Body Parameters and Query
You can combine body parameters with query parameters. Singular values without Body() become query parameters:
[Code Example]
URL: PUT /items/42/full?q=search
Request body:
[Code Example]
📦 5. Embed Single Body Parameter
For a single body parameter, FastAPI normally expects the model content directly. Use Body(embed=True) to wrap it in a JSON object:
[Code Example]
With embed=True, expects:
[Code Example]
Without embed=True, would expect:
[Code Example]
🌟 Key Concepts
Parameter Detection Rules
1. Path parameters: Declared in URL path with {parameter_name}
2. Query parameters: Singular values with defaults, not in path
3. Body parameters: Pydantic models or values with Body()
Request Body Structure
- Single Pydantic model: Direct model content (unless embed=True)
- Multiple Pydantic models: JSON object with model names as keys
- Mixed with Body(): Singular values added as additional keys
Validation Benefits
- All parameters validated according to their types
- Pydantic models provide rich validation and serialization
- Body() supports validation constraints (e.g., gt=0)
- Automatic OpenAPI documentation generation
💡 Best Practices
Import Organization
[Code Example]
Parameter Order
[Code Example]
Model Design
- Use Union[Type, None] for optional fields
- Provide sensible defaults for optional parameters
- Add validation constraints where appropriate
- Use descriptive field names and model names
🔗 What's Next?
In the next lesson, you'll learn about Body - Fields, where you'll discover how to add validation, metadata, and examples directly to Pydantic model fields using the Field() function.
📖 Additional Resources
- FastAPI Official Tutorial - Body - Multiple Parameters
- Pydantic Models Documentation
- FastAPI Body Parameters
- FastAPI Path Parameters
- FastAPI Query Parameters
Helpful Hint
FastAPI provides flexible ways to handle body parameters: multiple models create nested JSON, Body() adds singular values, and embed=True wraps single models.