ory, Design and Future  ](../../history-design-future/)
    * [ Benchmarks  ](../../benchmarks/)
  * [ Help  ](../../help/)

Help

    * [ Help FastAPI - Get Help  ](../../help-fastapi/)
    * [ Development - Contributing  ](../../contributing/)
  * [ Release Notes  ](../../release-notes/)

Table of contents

  * Use a ` Response ` parameter 
  * Return a ` Response ` directly 
  * Custom Headers 

  1. [ FastAPI  ](../..)
  2. [ Learn  ](../../learn/)
  3. [ Advanced User Guide  ](../)

#  Response Headers  ¶

##  Use a ` Response ` parameter  ¶

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.

    
    
    from fastapi import FastAPI, Response
    
    app = FastAPI()
    
    
    @app.get("/headers-and-object/")
    def get_headers(response: Response):
        response.headers["X-Cat-Dog"] = "alone in the world"
        return {"message": "Hello World"}
    

And then you can return any object you need, as you normally would (a ` dict `
, a database model, etc).

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.

##  Return a ` Response ` directly  ¶

You can also add headers when you return a ` Response ` directly.

Create a response as described in [ Return a Response Directly ](../response-
directly/) and pass the headers as an additional parameter:

    
    
    from fastapi import FastAPI
    from fastapi.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)
    

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 ` .

##  Custom Headers  ¶

Keep in mind that custom proprietary headers can be added [ using the 'X-'
prefix ](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) .

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) ](../../tutorial/cors/) ), using the parameter
` expose_headers ` documented in [ Starlette's CORS docs
](https://www.starlette.io/middleware/#corsmiddleware) .

The FastAPI trademark is owned by [ @tiangolo ](https://tiangolo.com) and is
registered in the US and across other regions

Made with [ Material for MkDocs ](https://squidfunk.github.io/mkdocs-
material/)

[ ](https://github.com/tiangolo/fastapi "github.com") [
](https://discord.gg/VQjSZaeJmf "discord.gg") [ ](https://twitter.com/fastapi
"twitter.com") [ ](https://www.linkedin.com/in/tiangolo "www.linkedin.com") [
](https://dev.to/tiangolo "dev.to") [ ](https://medium.com/@tiangolo
"medium.com") [ ](https://tiangolo.com "tiangolo.com")