I am building a middleware to log calls to functions among other things.
I have read about
FastAPI: How to get raw URL path from request?, which suggests to use request.scope.get("route")
, which works when used in the endpoint function. However, in the middleware, request.scope has no attribute “route”. How can i work around this ?
@app.get("/success/{id}", status_code=201)
def success():
return
app.add_middleware(RequestInfo)
from starlette.middleware.base import BaseHTTPMiddleware
from fastapi import Request
class RequestInfo(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
logger.info("Hello")
# Doing some things