Challenge - 5 Problems
Path Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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}
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type.
✗ Incorrect
FastAPI converts the path parameter 'item_id' to an integer because the function argument is typed as int. So the response JSON contains the integer 42.
📝 Syntax
intermediate2: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?Attempts:
2 left
💡 Hint
Path parameters must be included in the route string with curly braces and matched in the function parameters.
✗ Incorrect
Option A correctly uses curly braces to declare the path parameter and matches it in the function argument with the correct type. Option A treats user_id as a literal path segment. Option A uses invalid syntax for type hinting in the path. Option A misses the parameter in the function signature.
🔧 Debug
advanced2: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}
Attempts:
2 left
💡 Hint
Check the type of the path parameter and the value passed in the URL.
✗ Incorrect
FastAPI tries to convert the path parameter 'product_id' to int. The string 'abc' cannot be converted to int, so FastAPI returns a 422 error indicating the input is invalid.
❓ state_output
advanced2: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}
Attempts:
2 left
💡 Hint
Check the declared type of the path parameter and how FastAPI treats strings.
✗ Incorrect
The path parameter is typed as str, so FastAPI passes the exact string from the URL, including leading zeros.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI path parameters is true?
Select the correct statement about how FastAPI handles path parameters.
Attempts:
2 left
💡 Hint
Think about how FastAPI validates input before calling your function.
✗ Incorrect
FastAPI uses the type hints to convert and validate path parameters automatically. If conversion fails, it returns a 422 error before your function runs.