Skip to content

Commit

Permalink
fix: fast api response logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Nov 27, 2024
1 parent 1ea20bd commit dc38f53
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions tests/auth-react/fastapi-server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,21 @@
@app.middleware("http")
async def log_response(request: Request, call_next): # type: ignore
response = await call_next(request) # type: ignore
if isinstance(response, Response):
body = response.body
if isinstance(body, bytes):
print(f"Response: {body.decode('utf-8')}")

try:
body_bytes = b""
async for chunk in response.body_iterator: # type: ignore
body_bytes += chunk # type: ignore
print(f"Response: {body_bytes.decode('utf-8')}") # type: ignore
response_with_body = Response(
content=body_bytes,
status_code=response.status_code, # type: ignore
headers=response.headers, # type: ignore
media_type=response.media_type, # type: ignore
)
return response_with_body
except:
pass
return response # type: ignore


Expand Down

0 comments on commit dc38f53

Please sign in to comment.