Response Headers

    The current page still doesn’t have a translation for this language.

    But you can help translating it: Contributing.

    You can declare a parameter of type Response in your path operation function (as you can do for cookies).

    And then you can set headers in that temporal response object.

    And if you declared a response_model, it will still be used to filter and convert the object you returned.

    FastAPI will use that temporal response to extract the headers (also cookies and status code), and will put them in the final response that contains the value you returned, filtered by any response_model.

    You can also declare the Response parameter in dependencies, and set headers (and cookies) in them.

    You can also add headers when you return a directly.

    1. from fastapi import FastAPI
    2. app = FastAPI()
    3. def get_headers():
    4. headers = {"X-Cat-Dog": "alone in the world", "Content-Language": "en-US"}
    5. return JSONResponse(content=content, headers=headers)

    Technical Details

    You could also use from starlette.responses import Response or from starlette.responses import JSONResponse.

    FastAPI provides the same starlette.responses as fastapi.responses just as a convenience for you, the developer. But most of the available responses come directly from Starlette.

    And as the Response can be used frequently to set headers and cookies, FastAPI also provides it at fastapi.Response.

    But if you have custom headers that you want a client in a browser to be able to see, you need to add them to your CORS configurations (read more in CORS (Cross-Origin Resource Sharing)), using the parameter documented in .