Cleanup response.json() method.#2911
Conversation
👍 |
|
Sorry to bother all, but in my situation, changes in It cames from that I found some test failed in my new system. I diffed it with my old one, and found there was a difference between httpx from 0.27.0 to 0.22.0. The new system had the new version. So I had downgraded httpx from 0.27.0 to 0.22.0, and really resloved this bug. After that, I tried to found the reason. I delved into code, and thought it was def json(self, **kwargs: typing.Any) -> typing.Any:
if self.charset_encoding is None and self.content and len(self.content) > 3:
encoding = guess_json_utf(self.content)
print("==== encoding was: ", encoding)
if encoding is not None:
return jsonlib.loads(self.content.decode(encoding), **kwargs)
print("==== no encoding")
return jsonlib.loads(self.text, **kwargs)Then I guessed that using self.text is necessary in some uncommon situation. |
The server is likely returning non-compliant JSON. https://datatracker.ietf.org/doc/html/rfc8259#section-8.1
Take a look at what Also save If you're able to resolve the issue on the server-side, then do that because it appears to be a server-side issue. Otherwise use |
| encoding = guess_json_utf(self.content) | ||
| if encoding is not None: | ||
| return jsonlib.loads(self.content.decode(encoding), **kwargs) | ||
| return jsonlib.loads(self.text, **kwargs) |
There was a problem hiding this comment.
Just a random thing I noticed looking through the code, not a problem I've seen in the wild: isn't using self.text like this this better than the new version when there's a non-utf charset?
There was a problem hiding this comment.
JSON is spec'ed as always UTF encoded.
Co-authored-by: Kar Petrosyan <92274156+karpetrosyan@users.noreply.github.com>
The
Response.json()method appears unnecessarily complicated, I think this is a historical appendix that can be surgically tidied up. The stdlib implementation ofjson.loadsalready includes the charset detection that we're currently providing with our internalguess_json_utf()function.test_utilsto use a public API for testing JSON utf-8 encodings.guess_json_utf()function.Response.json()method.