question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to set response headers / cookies after yield

See original GitHub issue

It feels inconsistent that we can change headers / cookies in dependencies before yield, but can’t do this afterwords. E.g. in the following example only before=yield cookie will be set:

def set_cookie(response: Response):
    response.set_cookie("before", "yield")
    yield
    response.set_cookie("after", "yield")

@app.get("/")
def index(_=Depends(set_cookie)):
    return {}

The possibility of setting them “after request” would eliminate a need for middlewares in cases when their order is not important.

Alternatively we can at least throw an exception to make the behavior explicit.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sm-Fifteencommented, Oct 28, 2019

How would that work? Context manager dependencies are only closed once the response has been fully rendered, I believe, at which point it’s already far too late to add headers. Is there any reason why you wouldn’t be able to set both headers before yielding?

0reactions
michitarocommented, Sep 14, 2020

I found it when I tried to automatically save cookie-based session.

I’m guessing @imbolc were talking about codes something like this. It looks quite useful.

app = FastAPI()

def get_session_store(
    response: Response,
    FASTAPI_SESSION: Optional[str] = Cookie(None),
):
    store = {}
    if FASTAPI_SESSION is not None:
        store = json.loads(FASTAPI_SESSION)
    yield store
    response.set_cookie(key='FASTAPI_SESSION', value=json.dumps(store))


@app.get
def index(store: dict = Depends(get_session_store)):
    store['n'] = 42 # store will be automatically saved in cookie

@app.get
def clear(store: dict = Depends(get_session_store)):
    store.clear() # will work
    store = {'n': 42} # will not work

Is it possible to do something like this?

Anyway, Thank you very much for the nice framework!

Read more comments on GitHub >

github_iconTop Results From Across the Web

$http response Set-Cookie not accessible - Stack Overflow
4.7.4 The getAllResponseHeaders() method. Returns all headers from the response, with the exception of those whose field name is Set-Cookie or Set-Cookie2.
Read more >
Set-Cookie - HTTP - MDN Web Docs
The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent...
Read more >
Chapter 5. Cookies and response headers - CORS in Action
This chapter will introduce two new response headers: Access-Control-Allow-Credentials, which indicates that cookies may be included with requests, and Access- ...
Read more >
Response Headers - FastAPI - tiangolo
FastAPI will use that temporal response to extract the headers (also cookies and status code), and will put them in the final response...
Read more >
Working with COOKIES and HEADERS in Python SCRAPY ...
Hey what's up guys, in this video we gonna learn how to use cookies and headers along with scrapy spider's requests or the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found