Relative Content

Tag Archive for fastapistarlette

How to get custom middleware to run before AuthenticationMiddleware?

I’m sending an id_token in my cookies to a FastAPI application that also has a mounted Starlette app. I have a CustomMiddleware class that I’d like to run before the AuthenticationMiddleware for token validation. However, the AuthenticationMiddleware always runs first:

Get path template from starlette request from a middleware

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 ?