from fastapi import FastAPI
from fastapi.responses import Response, RedirectResponse
app = FastAPI()
@app.get("/")
def root():
return {"Hello": "World"}
@app.get("/cookieset")
def cookie_set(response: Response):
response.set_cookie(key="works", value="here is your data")
return "My cookies got set!"
@app.get("/cookiefail")
def cookie_fail(response: Response):
response.set_cookie(key="works", value="here is your data")
return RedirectResponse("/")
@app.get("/cookieset2")
def cookie_set2(response: Response):
# This works, but appears to fall short of what the documentation claims, noted below.
response.set_cookie(key="works", value="here is your data")
response.status_code = 307
response.headers["location"] = "/"
return response
@app.get("/cookieset3", response_class=RedirectResponse)
def cookie_set3() -> RedirectResponse:
# Proposed by @mcauto in comments below; sets cookie correctly. Pretty close to "ideal" behavior, I think the best behavior would be /cookieset4
response = RedirectResponse(url="/")
response.set_cookie(key="works", value="here is your data", domain="127.0.0.1")
return response
@app.get("/cookieset4", response_class=RedirectResponse)
def cookie_set4(response: RedirectResponse) -> RedirectResponse:
response.set_cookie(key="works", value="here is your data", domain="127.0.0.1")
return response
/cookieset.Delete the cookie from local storage.
Open the browser and call the endpoint /cookiefail.
Note the cookie is NOT set when the response is a redirect response (check local storage for cookies, it is empty)
The final /cookieset2 endpoint gives the correct behaviors, but the documentation here appears to suggest that setting cookies on the response argument will be correctly incorporated into the final response.
I would like cookies to be set when returning a RedirectResponse.
If you look at the documentation directly below where you linked, it shows that if you are returning a response instance, you must set the cookie on that instance:
https://fastapi.tiangolo.com/advanced/response-cookies/#return-a-response-directly
@coltonbh
Did you test your domain localhost?
@Mause it appears you are correct that the documentation states this behavior! I missed that. I still think this would be a nice feature for people as the behavior is a bit unexpected and I spent some time trying to diagnose it, but it is evidently documented. Should I close given that this behavior is in accordance with the documentation or should I leave it open as a feature request?
@mcauto what exactly do you mean? Yes, I was running this locally on my machine on localhost. Not sure what you mean "test your domain?" Can you clarify a bit? Thanks!
@coltonbh Sure, leave it as a feature request also you might want to rename the question with the [FEATURE] label.
@coltonbh
@app.get("/cookieset2", response_class=RedirectResponse)
def cookie_set2() -> RedirectResponse:
response = RedirectResponse(url="/")
response.set_cookie(key="works", value="here is your data", domain="127.0.0.1")
return response
@mcauto your example also does not set the cookie. I can add that to my question as another failing case.
@coltonbh
It's works.

@mcauto you are correct! I think I tested the wrong endpoint. Thanks!