How can I get the name
of a route/endpoint using FastAPI/Starlette? I have access to the Request
object and I need this information in one of my middlewares. For example, if I hit services/1
, I should then be able to get the abc
name. Is this possible in FastAPI?
@app.get("/services/{service}", name="abc") async def list_services() -> dict: do something
Update 1: Output of request.scope
{'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.3'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8001), 'client': ('127.0.0.1', 56670), 'scheme': 'http', 'root_path': '', 'headers': [(b'user-agent', b'PostmanRuntime/7.29.2'), (b'accept', b'*/*'), (b'postman-token', b'f2da2d0f-e721-44c8-b14f-e19750ea8a68'), (b'host', b'localhost:8001'), (b'accept-encoding', b'gzip, deflate, br'), (b'connection', b'keep-alive')], 'method': 'GET', 'path': '/health', 'raw_path': b'/health', 'query_string': b'', 'app': <fastapi.applications.FastAPI object at 0x1036d5790>}
Update 2: Providing middleware code where request.scope[“route”] is breaking.
from fastapi import FastAPI,Request app = FastAPI() @app.middleware("http") async def logging_middleware(request: Request, call_next): print(request.scope['route'].name) response = await call_next(request) return response @app.get('/', name='abc') def get_name(request: Request): return request.scope['route'].name
Advertisement
Answer
Option 1
You can get the name
value inside an endpoint as follows:
from fastapi import FastAPI,Request app = FastAPI() @app.get('/', name='abc') def get_name(request: Request): return request.scope['route'].name
Option 2
In a middleware, make sure to get the route’s name
after calling call_next(request)
, otherwise you would be faced with KeyError: 'route'
, as the route
key/object would not yet exist in the scope
dictionary. Example:
@app.middleware("http") async def some_middleware(request: Request, call_next): response = await call_next(request) print(request.scope['route'].name) return response
Option 3
Instead of a middleware, you could create a custom APIRoute
class, which would allow you to get the route’s name
before processing the request
and getting the response
(if that’s a requirement for your app). You could add an endpoint that you would like to be handled by that APIRoute
class using @<name_of_router_instance>
instead of @app
(e.g., @router.get('/', name='abc')
). More than one endpoint could be added in the same way. Example:
from fastapi import APIRouter, FastAPI, Request, Response from typing import Callable from fastapi.routing import APIRoute class CheckNameRoute(APIRoute): def get_route_handler(self) -> Callable: original_route_handler = super().get_route_handler() async def custom_route_handler(request: Request) -> Response: print(request.scope['route'].name) response = await original_route_handler(request) return response return custom_route_handler app = FastAPI() router = APIRouter(route_class=CheckNameRoute) @router.get('/', name='abc') def get_name(request: Request): return request.scope['route'].name app.include_router(router)