Challenge - 5 Problems
FastAPI Route Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Route Matching Priority in FastAPI
Given these two routes in a FastAPI app, which route will handle a GET request to
/items/42?FastAPI
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: int): return {"route": "item_id route", "id": item_id} @app.get('/items/special') async def read_special(): return {"route": "special route"}
Attempts:
2 left
💡 Hint
Consider how FastAPI matches dynamic path parameters versus static paths.
✗ Incorrect
FastAPI matches routes in the order they are defined. The request '/items/42' matches the first route '/items/{item_id}', so the 'read_item' function handles it.
📝 Syntax
intermediate2:00remaining
Correct Route Declaration for Overlapping Paths
Which option correctly declares two FastAPI routes so that
/users/me returns a special response and /users/{user_id} returns a user by ID without conflict?Attempts:
2 left
💡 Hint
Static routes should be declared before dynamic routes to avoid conflicts.
✗ Incorrect
FastAPI matches routes in the order they are declared. Declaring the static route '/users/me' before the dynamic '/users/{user_id}' ensures '/users/me' is matched first and does not get captured by the dynamic route.
🔧 Debug
advanced2:00remaining
Unexpected Route Matching Behavior
A FastAPI app has these routes declared in this order. A GET request to
/files/download returns the response from which route?FastAPI
from fastapi import FastAPI app = FastAPI() @app.get('/files/{file_name}') async def get_file(file_name: str): return {"file": file_name} @app.get('/files/download') async def download_file(): return {"action": "download"}
Attempts:
2 left
💡 Hint
Check the order of route declarations and how FastAPI matches dynamic routes.
✗ Incorrect
FastAPI matches routes in the order they are declared. Since '/files/{file_name}' is declared before '/files/download', the request '/files/download' matches the dynamic route first, treating 'download' as the file_name parameter.
❓ state_output
advanced2:00remaining
Route Response Based on Declaration Order
Consider this FastAPI app. What JSON response will a GET request to
/products/latest return?FastAPI
from fastapi import FastAPI app = FastAPI() @app.get('/products/{product_id}') async def get_product(product_id: str): return {"product_id": product_id} @app.get('/products/latest') async def get_latest_product(): return {"product": "latest"}
Attempts:
2 left
💡 Hint
Route order affects which handler is called for overlapping paths.
✗ Incorrect
Since '/products/{product_id}' is declared before '/products/latest', the request '/products/latest' matches the dynamic route first, returning the product_id as 'latest'.
🧠 Conceptual
expert3:00remaining
Ensuring Static Routes Take Priority Over Dynamic Ones
In FastAPI, how can you guarantee that a static route like
/api/status is always matched before a dynamic route like /api/{param} regardless of their declaration order?Attempts:
2 left
💡 Hint
FastAPI does not have a built-in priority parameter; think about extending routing behavior.
✗ Incorrect
FastAPI matches routes in the order they are declared. To guarantee static routes always take priority regardless of order, you must customize the routing logic, for example by using a custom APIRouter with a matcher that prioritizes static routes over dynamic ones.