Ability to set response headers / cookies after yield
See original GitHub issueIt 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:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
I’m guessing @imbolc were talking about codes something like this. It looks quite useful.
Is it possible to do something like this?
Anyway, Thank you very much for the nice framework!