0
0
FastAPIframework~20 mins

Multiple path parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Body Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending JSON with two body parameters?

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?

FastAPI
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}
A422 Unprocessable Entity error
B{"name": "Book", "description": "A nice book"}
C{"name": "Book"}
D{"description": "A nice book"}
Attempts:
2 left
💡 Hint

Think about how FastAPI extracts multiple body parameters from JSON.

📝 Syntax
intermediate
2:00remaining
Which code correctly defines multiple body parameters in FastAPI?

Choose the correct FastAPI endpoint definition that accepts two required string parameters title and content from the request body.

Aasync def post_article(title: str = Body(...), content: str = Body(...)):
Basync def post_article(title: str, content: str):
Casync def post_article(body: dict): title = body['title']; content = body['content']
Dasync def post_article(title: str = Query(...), content: str = Query(...)):
Attempts:
2 left
💡 Hint

Remember that Body(...) is needed to extract parameters from the JSON body.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error?

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?

FastAPI
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}
A400 Bad Request because 'name' is missing
B200 OK with {'name': 'Alice', 'age': None}
C422 Unprocessable Entity because 'age' is missing
D500 Internal Server Error due to missing age
Attempts:
2 left
💡 Hint

Check which parameters are required and what happens if one is missing.

state_output
advanced
2:00remaining
What is the response when mixing body and query parameters?

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?

FastAPI
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}
A{"name": "Laptop", "category": "electronics"}
B{"name": "Laptop"}
C422 Unprocessable Entity error
D{"category": "electronics"}
Attempts:
2 left
💡 Hint

Recall how FastAPI separates body and query parameters.

🧠 Conceptual
expert
3:00remaining
Why does FastAPI require explicit Body(...) for multiple body parameters?

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?

ABecause <code>Body(...)</code> is required to validate query parameters
BBecause FastAPI does not support JSON bodies without <code>Body(...)</code> annotations
CBecause Python functions cannot have more than one parameter from the body without <code>Body(...)</code>
DBecause HTTP requests can only have one JSON body, so FastAPI needs explicit markers to parse multiple fields separately
Attempts:
2 left
💡 Hint

Think about how HTTP request bodies work and how FastAPI maps them to function parameters.