Fastapi: [FEATURE] Set cookies when returning a RedirectResponse

Created on 1 Dec 2020  路  8Comments  路  Source: tiangolo/fastapi

Example

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

Description

  • Open the browser and call the endpoint /cookieset.
  • Note the cookie is properly set in localstorage
  • 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.

The solution you would like

I would like cookies to be set when returning a RedirectResponse.

Environment

  • OS: macOS:
  • FastAPI Version 0.62.0:
  • Python version: 3.9.0
enhancement

All 8 comments

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.

image

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RogerioDosSantos picture RogerioDosSantos  路  3Comments

mr-bjerre picture mr-bjerre  路  3Comments

updatatoday picture updatatoday  路  3Comments

DrPyser picture DrPyser  路  3Comments

iwoloschin picture iwoloschin  路  3Comments