[Python] FastAPI RedirectResponse custom headers [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página