Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
FastAPI : Retrieving variable from group of path operation dependency

I would like to manage a custom authorization system in my FastAPI app.

To achieve this, I would like to use group path operations dependencies on concerned routes to ensure the auth token is present and valid. I followed this guide in documentation that works well.

But since my business logic is more complicated, I also would like to be able to use the API token in my controlers to perform additional rules.

The tip in this section states that Note that, much like dependencies in path operation decorators, no value will be passed to your path operation function.

Which means, if the auth token is defined as a dependency for a group of path operations, there's no way to retrieve its value in the controlers.

Does it mean that I necessarily have to use single dependencies in each path operation function by adding them as a parameter, or is there another way of doing this in a more DRY way ? I know I can retrieve the header within request.headers, but is it how it's supposed to be done ?

One way would look like this :

router = APIRouter()

@router.get('/test')
async def test(request: Request, token: str = Depends(check_token)):
    token  # directly available

While the other is :

router = APIRouter(dependencies=[Depends(check_token)])

@router.get('/test')
async def test(request: Request):
    token = request.headers['auth']  # need some work around to retrieve token
    ...
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda