-
Notifications
You must be signed in to change notification settings - Fork 388
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
Fix not working for binary response in httpx #574
base: master
Are you sure you want to change the base?
Conversation
except (TypeError, UnicodeDecodeError, AttributeError): | ||
# Sometimes the string actually is binary or StringIO object, | ||
# so if you can't decode it, just give up. | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, httpx
can take bytes
. That's why the content
is encoded back to bytes
in _from_serialized_response
when constructing httpx.Response
. The decode is for serializing the VCR response, not for httpx
. If you take out the decode, I believe you will get !!binary
in the serialized yaml even for plain text. I guess reason of decoding the data into unicode is trying to have human readable yaml file. This unicode / bytes conversion happens in other libraries as well, but it is done in for the body field only. Therefore, I suggest the better fix should be having a consistent serialization schema for all libraries, which is discussed in #463.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I tested, httpx.Response.content
can deal with bytes type. So it wouldn't be necessary to try to decode
df3997c
to
34d5384
Compare
Also fixed in #784 |
I suggest we close this one in view of #784 |
The
httpx_reponse.content.decode("utf-8", "ignore")
would corrupt binary content that can't decoded to unicode. We should serialize the binary content directly in that case.A better way to fix this is probably is set the content to
response["body"]["string"]
becausevcr.serializers.compat.convert_to_unicode()
andvcr.serializers.compat.convert_to_bytes()
handle the bytes / unicode conversion of that field. But that will change the cassette format for httpx. I guess you guys can consider that when implementing #463.