Coder Social home page Coder Social logo

Comments (4)

lazka avatar lazka commented on June 9, 2024 1

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.

lazka avatar lazka commented on June 9, 2024

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.

steinitzu avatar steinitzu commented on June 9, 2024

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.

lazka avatar lazka commented on June 9, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.