0
0
FastAPIframework~20 mins

Path parameters in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when accessing a path with a parameter?
Consider this FastAPI code snippet. What will be the response when a client requests /items/42?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}
A{"item_id": "42"}
B{"item_id": 42}
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a path parameter in FastAPI?
Which of the following FastAPI route definitions correctly declares a path parameter named user_id of type str?
A
@app.get("/users/{user_id}")
async def get_user(user_id: str):
    return {"user_id": user_id}
B
@app.get("/users/user_id")
async def get_user(user_id: str):
    return {"user_id": user_id}
C
@app.get("/users/{user_id:int}")
async def get_user(user_id: str):
    return {"user_id": user_id}
D
@app.get("/users/{user_id}")
async def get_user():
    return {"user_id": user_id}
Attempts:
2 left
💡 Hint
Path parameters must be included in the route string with curly braces and matched in the function parameters.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI path parameter cause a 422 error?
Given this code, why does a request to /products/abc return a 422 Unprocessable Entity error?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/products/{product_id}")
async def get_product(product_id: int):
    return {"product_id": product_id}
ABecause the return statement is missing a JSON response model.
BBecause the route path is missing a trailing slash.
CBecause the function is missing an async keyword.
DBecause 'abc' cannot be converted to int as required by the path parameter type.
Attempts:
2 left
💡 Hint
Check the type of the path parameter and the value passed in the URL.
state_output
advanced
2:00remaining
What is the value of user_id after this request?
Given this FastAPI route, what will be the value of user_id inside the function when a client requests /users/007?
FastAPI
from fastapi import FastAPI

app = FastAPI()

@app.get("/users/{user_id}")
async def read_user(user_id: str):
    return {"user_id": user_id}
A"7" (string, leading zeros removed)
B7 (integer, leading zeros removed)
C"007" (string with leading zeros preserved)
DError because of leading zeros in path parameter
Attempts:
2 left
💡 Hint
Check the declared type of the path parameter and how FastAPI treats strings.
🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI path parameters is true?
Select the correct statement about how FastAPI handles path parameters.
AFastAPI automatically converts path parameters to the declared type and returns a 422 error if conversion fails.
BFastAPI treats all path parameters as strings and never performs type conversion.
CFastAPI requires explicit type conversion inside the route function for path parameters.
DFastAPI only supports integer path parameters and rejects others.
Attempts:
2 left
💡 Hint
Think about how FastAPI validates input before calling your function.