Currently, the logging middleware uses json.loads(response.body) to parse the response body for logging purposes. This approach is inefficient as it requires reading and parsing the response stream.
Proposed Solution
Use a custom response class to store the original response data, avoiding the need to re-parse the JSON. This can be implemented following the approach discussed in this FastAPI discussion.
References