From 12fb0663d1ecc67832b28ed107935a97432216c1 Mon Sep 17 00:00:00 2001 From: Emmanuel Jaep <2668031+jaepetto@users.noreply.github.com> Date: Tue, 25 Oct 2022 16:04:23 +0200 Subject: [PATCH] FIX content-length header gets incorrectly set to 0 by default --- src/fastapi_redis_cache/cache.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/fastapi_redis_cache/cache.py b/src/fastapi_redis_cache/cache.py index 5257a14..d2cfbd2 100644 --- a/src/fastapi_redis_cache/cache.py +++ b/src/fastapi_redis_cache/cache.py @@ -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.