Response Headers

You can add headers to your response.

Create a response as described in Return a Response directly and pass the headers as an additional parameter:

from fastapi import FastAPI
from starlette.responses import JSONResponse

app = FastAPI()


@app.get("/headers/")
def get_headers():
    content = {"message": "Hello World"}
    headers = {"X-Cat-Dog": "alone in the world", "Content-Language": "en-US"}
    return JSONResponse(content=content, headers=headers)

Tip

Have in mind that custom proprietary headers can be added using the 'X-' prefix.

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, using the parameter expose_headers documented in Starlette's CORS docs.