Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

317
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda