Skip to content

Commit

Permalink
MP-432 Fix You cannot access body after reading from request's data s…
Browse files Browse the repository at this point in the history
…tream
  • Loading branch information
Sheripov committed Sep 14, 2023
1 parent 97cf553 commit 893baf2
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions django_google_structured_logger/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SetRequestToLoggerMiddleware:
def __init__(self, get_response):
# One-time configuration and initialization.
self.get_response = get_response
self.request_body = None

def __call__(self, request: HttpRequest) -> HttpResponse:
# Early exit if logging middleware is disabled
Expand All @@ -28,6 +29,7 @@ def __call__(self, request: HttpRequest) -> HttpResponse:
# Code to be executed for each request before
# the view (and later middleware) are called.

self.request_body = getattr(request, "body", None)
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
Expand Down Expand Up @@ -256,13 +258,9 @@ def decode_and_abridge(body_bytes):
case "multipart/form-data":
return "The image was uploaded to the server"
case "application/json":
return self._mask_sensitive_data(
decode_and_abridge(getattr(request, "body", None))
)
return self._mask_sensitive_data(decode_and_abridge(self.request_body))
case "text/plain":
return self._mask_sensitive_data(
self._abridge(getattr(request, "body", None))
)
return self._mask_sensitive_data(self._abridge(self.request_body))
case _:
return self._mask_sensitive_data(content_type)

Expand Down

0 comments on commit 893baf2

Please sign in to comment.