--- /dev/null
+from fastapi import FastAPI
+from starlette.responses import JSONResponse
+
+app = FastAPI()
+
+
+@app.post("/cookie/")
+def create_cookie():
+ content = {"message": "Come to the dark side, we have cookies"}
+ response = JSONResponse(content=content)
+ response.set_cookie(key="fakesession", value="fake-cookie-session-value")
+ return response
--- /dev/null
+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
This technique is used in the tutorial about <a href="https://fastapi.tiangolo.com/tutorial/sql-databases/" target="_blank">SQL (Relational) Databases</a>.
+
+!!! tip
+ Have in mind that custom proprietary headers can be added <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" target="_blank">using the 'X-' prefix</a>.
+
+ 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 <a href="https://fastapi.tiangolo.com/tutorial/cors/" target="_blank">CORS configurations</a>, using the parameter `expose_headers` documented in <a href="https://www.starlette.io/middleware/#corsmiddleware" target="_blank">Starlette's CORS docs</a>.
+
### Before and after the `response`
You can add code to be run with the `request`, before any *path operation* receives it.
--- /dev/null
+You can create (set) Cookies in your response.
+
+To do that, you can create a response as described in <a href="https://fastapi.tiangolo.com/tutorial/response-directly/" target="_blank">Return a Response directly</a>.
+
+Then set Cookies in it, and then return it:
+
+```Python hl_lines="10 11 12"
+{!./src/response_cookies/tutorial001.py!}
+```
+
+## More info
+
+To see all the available parameters and options, check the <a href="https://www.starlette.io/responses/#set-cookie" target="_blank">documentation in Starlette</a>.
--- /dev/null
+You can add headers to your response.
+
+Create a response as described in <a href="https://fastapi.tiangolo.com/tutorial/response-directly/" target="_blank">Return a Response directly</a> and pass the headers as an additional parameter:
+
+```Python hl_lines="10 11 12"
+{!./src/response_headers/tutorial001.py!}
+```
+
+!!! tip
+ Have in mind that custom proprietary headers can be added <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers" target="_blank">using the 'X-' prefix</a>.
+
+ 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 <a href="https://fastapi.tiangolo.com/tutorial/cors/" target="_blank">CORS configurations</a>, using the parameter `expose_headers` documented in <a href="https://www.starlette.io/middleware/#corsmiddleware" target="_blank">Starlette's CORS docs</a>.
- Return a Response directly: 'tutorial/response-directly.md'
- Custom Response Class: 'tutorial/custom-response.md'
- Additional Responses in OpenAPI: 'tutorial/additional-responses.md'
+ - Response Cookies: 'tutorial/response-cookies.md'
+ - Response Headers: 'tutorial/response-headers.md'
- Dependencies:
- First Steps: 'tutorial/dependencies/first-steps.md'
- Classes as Dependencies: 'tutorial/dependencies/classes-as-dependencies.md'
--- /dev/null
+from starlette.testclient import TestClient
+
+from response_cookies.tutorial001 import app
+
+client = TestClient(app)
+
+
+def test_path_operation():
+ response = client.post("/cookie/")
+ assert response.status_code == 200
+ assert response.json() == {"message": "Come to the dark side, we have cookies"}
+ assert response.cookies["fakesession"] == "fake-cookie-session-value"
--- /dev/null
+from starlette.testclient import TestClient
+
+from response_headers.tutorial001 import app
+
+client = TestClient(app)
+
+
+def test_path_operation():
+ response = client.get("/headers/")
+ assert response.status_code == 200
+ assert response.json() == {"message": "Hello World"}
+ assert response.headers["X-Cat-Dog"] == "alone in the world"
+ assert response.headers["Content-Language"] == "en-US"