Skip to content

Failed response.json() call in _helpers.response_log() causes response object's state to change #1760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
anarkiwi opened this issue May 8, 2025 · 0 comments
Assignees

Comments

@anarkiwi
Copy link

anarkiwi commented May 8, 2025

Hello,

In v2.40, new helpers were introduced that can log various requests and responses.

One such helper, response_log() tries to jsonify a response instance (

json_response = response.json()
) and use the result to log.

The issue with this is it causes the response instance state to change. Called via https://github.com/psf/requests/blob/c65c780849563c891f35ffc98d3198b71011c012/src/requests/models.py#L965, we can retrieve content which causes _content_consumed to change to True, even though the JSON decode failed.

This means it isn't safe to call json() on the response and keep the response unchanged. I can see the argument that this is a bug in requests, but for the moment there is now a subtle side effect introduced just by attempting logging - the implicit assumption is response won't change. I can imagine a less than ideal option would be to copy the response before trying to json()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants