Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX content-length header gets incorrectly set to 0 by default #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/fastapi_redis_cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ async def inner_wrapper(*args, **kwargs):
create_response_directly = not response
if create_response_directly:
response = Response()
# The response may get created with a 'content-length' header equal to 0. Of course, if the `func` function does return something, this header becomes a mismatch with the actual content.
# uvicorn is sensible to it and then raises a 'RuntimeError: Response content longer than Content-Length' exception.
# As a workaround, the 'content-length' header is cleared from the response.headers if it exists
if "content-length" in response.headers.keys():
del response.headers["content-length"]
redis_cache = FastApiRedisCache()
if redis_cache.not_connected or redis_cache.request_is_not_cacheable(request):
# if the redis client is not connected or request is not cacheable, no caching behavior is performed.
Expand Down