FastAPI reads the JSON body, extracts multiple parameters, passes them to the function, and returns a response.
Execution Sample
FastAPI
from fastapi import FastAPI, Body
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.post("/items/")
asyncdef create_item(item: Item = Body(...), quantity: int = Body(...)):
return {"item_name": item.name, "total_price": item.price * quantity}
This code defines a POST endpoint that accepts an item object and a quantity from the request body and returns the total price.
Execution Table
Step
Action
Input Data
Parameter Parsed
Function Output
1
Receive POST request
{"name": "Book", "price": 12.5, "quantity": 3}
None yet
None yet
2
Parse 'item' parameter
{"name": "Book", "price": 12.5}
Item(name='Book', price=12.5)
None yet
3
Parse 'quantity' parameter
3
quantity=3
None yet
4
Call create_item function
item=Item(name='Book', price=12.5), quantity=3
Both parameters ready
Return {'item_name': 'Book', 'total_price': 37.5}
5
Send response
Return data
N/A
{"item_name": "Book", "total_price": 37.5}
💡 All parameters parsed and function executed, response sent to client.
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
Final
item
None
Item(name='Book', price=12.5)
Item(name='Book', price=12.5)
Item(name='Book', price=12.5)
Item(name='Book', price=12.5)
quantity
None
None
3
3
3
output
None
None
None
{"item_name": "Book", "total_price": 37.5}
{"item_name": "Book", "total_price": 37.5}
Key Moments - 3 Insights
Why do we need to use a Pydantic model for the 'item' parameter?
Because FastAPI uses Pydantic models to validate and parse complex JSON objects from the request body, as shown in step 2 of the execution_table.
How does FastAPI know which parameters come from the body?
FastAPI infers that Pydantic models come from the body automatically, and simple types like 'int' can also come from the body if declared as parameters with Body(), as seen in steps 2 and 3.
Can we have multiple parameters from the body without using a Pydantic model?
No, FastAPI expects only one body parameter by default. To have multiple, you wrap them in a Pydantic model or use special techniques like declaring parameters with Body(). Here, 'quantity' is a simple parameter but must be declared properly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'quantity' after Step 3?
A3
BNone
CItem(name='Book', price=12.5)
D0
💡 Hint
Check the 'quantity' column in variable_tracker after Step 3.
At which step does the function 'create_item' return the output?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look at the 'Function Output' column in execution_table.
If the 'quantity' parameter was missing in the request, what would happen?
AThe function would receive quantity=0 automatically.
BThe function would receive quantity=None without error.
CFastAPI would raise a validation error before calling the function.
DThe response would be empty.
💡 Hint
FastAPI requires all declared body parameters unless default values are set.
Concept Snapshot
FastAPI Body with multiple parameters:
- Use Pydantic models for complex JSON body parts.
- Declare multiple parameters in function signature with Body().
- FastAPI parses each from request body.
- Function receives parsed parameters.
- Returns response using processed data.
Full Transcript
This visual execution shows how FastAPI handles a POST request with multiple parameters in the body. The client sends JSON including an item object and a quantity. FastAPI first parses the 'item' parameter using the Pydantic model, then parses the 'quantity' integer. After both are ready, FastAPI calls the endpoint function with these parameters. The function calculates the total price and returns a JSON response. Variables 'item' and 'quantity' change from None to their parsed values step by step. Key points include the need for Pydantic models for complex data and how FastAPI infers body parameters. The quiz questions check understanding of parameter parsing and function execution steps.