0
0
FastAPIframework~20 mins

Route ordering and priority in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Route Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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"}
AFastAPI raises a runtime error due to ambiguous routes.
BThe route '/items/special' handles the request.
CBoth routes handle the request simultaneously.
DThe route '/items/{item_id}' handles the request.
Attempts:
2 left
💡 Hint
Consider how FastAPI matches dynamic path parameters versus static paths.
📝 Syntax
intermediate
2: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?
A
@app.get('/users/me')
async def get_me():
    return {"user": "current user"}

@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}

@app.get('/users/me')
async def get_me():
    return {"user": "current user"}
C
@app.get('/users/{user_id}')
async def get_user(user_id: str):
    return {"user_id": user_id}

@app.get('/users/{me}')
async def get_me():
    return {"user": "current user"}
D
@app.get('/users/me')
async def get_me():
    return {"user": "current user"}

@app.get('/users/{id}')
async def get_user(id: str):
    return {"user_id": id}
Attempts:
2 left
💡 Hint
Static routes should be declared before dynamic routes to avoid conflicts.
🔧 Debug
advanced
2: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"}
AThe request results in a 404 Not Found error.
BThe '/files/{file_name}' route handles the request with file_name='download'.
CFastAPI raises a runtime error due to route conflict.
DThe '/files/download' route handles the request.
Attempts:
2 left
💡 Hint
Check the order of route declarations and how FastAPI matches dynamic routes.
state_output
advanced
2: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"}
A{"product_id": "latest"}
B{"product": "latest"}
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Route order affects which handler is called for overlapping paths.
🧠 Conceptual
expert
3: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?
AUse the <code>include_in_schema=False</code> parameter on the dynamic route.
BUse the <code>priority</code> parameter in the route decorator to set static routes higher.
CUse a custom APIRouter with a route matcher that prioritizes static routes.
DDeclare the static route before the dynamic route in the code.
Attempts:
2 left
💡 Hint
FastAPI does not have a built-in priority parameter; think about extending routing behavior.