Comments (4)
You could add the headers from response dependency to your new response like this, should work:
Thanks! I'll give that a try.
This worked nicely, thanks again
from fastapi-etag.
I've solved this with a middleware for now:
def get_etag():
return "foo"
@app.middleware("http")
async def add_etags_everywhere(request: Request, call_next: Callable) -> Response:
etag = request.headers.get("if-none-match")
current_etag = get_etag()
if etag == current_etag:
return Response("", 304, headers={"etag": current_etag})
response: Response = await call_next(request)
if "etag" not in response.headers:
response.headers["etag"] = current_etag
return response
from fastapi-etag.
The problem is that returning a response object from the route completely overrides any changes to the response made in the dependency chain.
You could add the headers from response dependency to your new response like this, should work:
app.get('/foobar', dependencies=[Depends(Etag(get_etag))])
async def repos(request: Request, response: Response) -> Any:
return JSONResponse([], headers=response.headers)
But the middleware approach is probably better if you're adding etags the same way on every route
from fastapi-etag.
You could add the headers from response dependency to your new response like this, should work:
Thanks! I'll give that a try.
Feel free to close this (and maybe add this to the doc?, btw the examples in the docs don't work due to missing Depends usage and import)
But the middleware approach is probably better if you're adding etags the same way on every route
That sadly didn't work out because I hit tiangolo/fastapi#1472
from fastapi-etag.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastapi-etag.