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

482
Visualizações
FastAPI RedirectResponse custom headers

Based off of my previous question, I need to now add a header to the response.

According to the documentation, I can simply just add the headers and another attribute to the RedirectResponse object.

When I test this, it doesn't seem to carry the header value over.

According to this post, it is impossible to set headers for a redirect request. So instead of a Redirect, maybe I should try something else?

Any ideas?

from fastapi import FastAPI, Request
from starlette.responses import RedirectResponse

app = FastAPI()

@app.get("/data/")
async def api_data(request: Request):
    params = str(request.query_params)
    url = f'http://some.other.api/{params}'
    headers = {'Authorization': "some_long_key"}
    response = RedirectResponse(url=url, headers=headers)
    return response
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

One solution is to do the request in the app and then return the response

BUT this is very inefficient and will force all traffic to come through this server, while a redirect would have less network load

import requests
from fastapi import FastAPI, Request, Response

app = FastAPI()

@app.get("/data/")
async def api_data(request: Request):
    params = str(request.query_params)
    url = f'http://some.other.api/{params}'
    headers = {'Authorization': "some_long_key"}
    r = requests.get(url, headers=headers)
    return Response(content=r.content)
over 4 years ago · Santiago Trujillo Relatório
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