Performance: Path parameters
MEDIUM IMPACT
Path parameters affect the routing speed and server response time, impacting how quickly the server matches URLs to handlers.
from fastapi import FastAPI, Path app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: int = Path(..., gt=0)): # Type and constraint validation improves routing return {"item_id": item_id}
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: str): # No type validation or constraints return {"item_id": item_id}
| Pattern | Routing Cost | Validation Cost | Server Processing | Verdict |
|---|---|---|---|---|
| Generic string path parameter | High (broad matching) | Low (simple) | Moderate | [X] Bad |
| Typed path parameter with constraints | Low (precise matching) | Moderate (validation) | Low | [OK] Good |