Skip to content

Commit a9eb001

Browse files
committed
revert solution for caching requests payload as it was conflicting on reusing the request body
1 parent 329ef2f commit a9eb001

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

fastapi_gae_logging/fastapi_gae_logging.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,24 +249,12 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
249249
"""
250250
if scope["type"] == "http":
251251

252-
# https://stackoverflow.com/questions/64115628/get-starlette-request-body-in-the-middleware-context
253-
done = False
254-
chunks: list[bytes] = []
255-
256-
async def wrapped_receive() -> Message:
257-
nonlocal done
258-
message = await receive()
259-
if message["type"] == "http.disconnect":
260-
done = True
261-
return message
262-
body = message.get("body", b"")
263-
more_body = message.get("more_body", False)
264-
if not more_body:
265-
done = True
266-
chunks.append(body)
267-
return message
268-
269-
request = Request(scope, receive=wrapped_receive)
252+
_receive_cache = await receive()
253+
254+
async def receive_cache():
255+
return _receive_cache
256+
257+
request = Request(scope, receive=receive_cache)
270258

271259
gae_request_context.set({
272260
'trace': request.headers.get('X-Cloud-Trace-Context'),
@@ -287,7 +275,7 @@ async def send_spoof_wrapper(message: Dict[str, Any]) -> None:
287275
await send(message)
288276

289277
try:
290-
await self.app(scope, wrapped_receive, send_spoof_wrapper)
278+
await self.app(scope, receive_cache, send_spoof_wrapper)
291279
except Exception as e:
292280
if not isinstance(e, HTTPException):
293281
logging.exception(e)

0 commit comments

Comments
 (0)