Consider this FastAPI endpoint that expects two body parameters:
from fastapi import FastAPI, Body
app = FastAPI()
@app.post("/items/")
async def create_item(name: str = Body(...), description: str = Body(...)):
return {"name": name, "description": description}If you send a POST request with JSON body {"name": "Book", "description": "A nice book"}, what will be the response?
from fastapi import FastAPI, Body app = FastAPI() @app.post("/items/") async def create_item(name: str = Body(...), description: str = Body(...)): return {"name": name, "description": description}
Think about how FastAPI extracts multiple body parameters from JSON.
FastAPI can extract multiple parameters from the JSON body if each is declared with Body(...). The JSON keys must match the parameter names. Here, both name and description are required, so sending both returns them in the response.
Choose the correct FastAPI endpoint definition that accepts two required string parameters title and content from the request body.
Remember that Body(...) is needed to extract parameters from the JSON body.
Option A correctly uses Body(...) to declare that title and content come from the request body. Option A misses Body, so FastAPI treats them as query parameters. Option A is invalid syntax for FastAPI parameters. Option A uses Query which extracts from URL query, not body.
Given this FastAPI endpoint:
from fastapi import FastAPI, Body
app = FastAPI()
@app.post("/users/")
async def create_user(name: str = Body(...), age: int = Body(...)):
return {"name": name, "age": age}If you send JSON {"name": "Alice"} only, what error occurs and why?
from fastapi import FastAPI, Body app = FastAPI() @app.post("/users/") async def create_user(name: str = Body(...), age: int = Body(...)): return {"name": name, "age": age}
Check which parameters are required and what happens if one is missing.
Both name and age are required body parameters (due to Body(...)). Missing age causes FastAPI to return a 422 error indicating the request body is incomplete.
Consider this FastAPI endpoint:
from fastapi import FastAPI, Body, Query
app = FastAPI()
@app.post("/products/")
async def add_product(name: str = Body(...), category: str = Query(...)):
return {"name": name, "category": category}If you send a POST request with JSON body {"name": "Laptop"} and URL query ?category=electronics, what is the response?
from fastapi import FastAPI, Body, Query app = FastAPI() @app.post("/products/") async def add_product(name: str = Body(...), category: str = Query(...)): return {"name": name, "category": category}
Recall how FastAPI separates body and query parameters.
The name parameter is extracted from the JSON body, and category is extracted from the URL query string. Both are required, so sending both correctly returns them in the response.
FastAPI allows only one parameter to be taken from the request body by default. When you want multiple parameters from the body, you must use Body(...) explicitly for each. Why is this design chosen?
Think about how HTTP request bodies work and how FastAPI maps them to function parameters.
HTTP requests have a single body, usually JSON. FastAPI needs to know which parameters come from that single JSON body. By default, it expects one body parameter. To extract multiple fields separately, Body(...) tells FastAPI to look inside the JSON body for each parameter explicitly.