python
runtime_error
ai_generated
true
RuntimeError: No route matches for path '/api/items/123'
ID: python/starlette-runtimeerror-no-route-match
80%Fix Rate
82%Confidence
0Evidence
2024-05-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
The requested URL path does not match any defined route in the Starlette application.
generic中文
请求的 URL 路径与 Starlette 应用程序中定义的任何路由不匹配。
Workarounds
-
95% success Define a route with path parameter.
from starlette.applications import Starlette from starlette.responses import JSONResponse app = Starlette() @app.route('/api/items/{item_id}') async def get_item(request): return JSONResponse({'id': request.path_params['item_id']}) -
90% success Add a 404 handler for unmatched routes.
from starlette.responses import PlainTextResponse @app.exception_handler(404) async def not_found(request, exc): return PlainTextResponse('Not Found', status_code=404)
Dead Ends
Common approaches that don't work:
-
Adding a catch-all route without proper order.
50% fail
Catch-all routes must be defined last; otherwise they override specific routes.
-
Using regular expression in route path incorrectly.
60% fail
Starlette uses path parameters with curly braces, not regex. Misuse leads to no match.