[Python] Can't access path parameters from middleware

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    My typical path is something like

    /user/{user_id}/resource/{resource_id}


    I have a validation method, already written in async Python, like this:

    async def is_allowed(user_id: int, resource_id: int) -> bool


    That returns a boolean: True if the user can access the resource, False otherwise.

    I want to write a middleware that calls is_allowed extracting the variables from the path.

    I fiddled around but I can't find how to get them: I was expecting to get this information from request.path_params.

    A more complete example:

    import logging

    from fastapi import FastAPI
    from starlette.requests import Request
    from starlette.responses import Response

    app = FastAPI()

    _logger = logging.getLogger()
    _logger.setLevel(logging.DEBUG)


    async def is_allowed(user_id, resource_id):
    _logger.error(user_id)
    _logger.error(resource_id)
    return True


    @app.middleware('http')
    async def acl(request: Request, call_next):
    user_id = request.path_params.get("user_id", None)
    resource_id = request.path_params.get("resource_id", None)
    allowed = await is_allowed(user_id, resource_id)
    if not allowed:
    return Response(status_code=403)
    else:
    return await call_next(request)


    @app.get('/user/{user_id}/resource/{resource_id}')
    async def my_handler(user_id: int, resource_id: int):
    return {"what": f"Doing stuff with {user_id} on {resource_id}"}


    The logged values are None.

    Continue reading...

Compartilhe esta Página