Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yhlam
Copy link

@yhlam yhlam commented Jan 27, 2021

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"] because vcr.serializers.compat.convert_to_unicode() and vcr.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.

except (TypeError, UnicodeDecodeError, AttributeError):
# Sometimes the string actually is binary or StringIO object,
# so if you can't decode it, just give up.
pass

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @yhlam , I think we could completely ignore this decode, as I did in my PR #583

As you can see on httpx, the content attribute can be a bytes type.

Copy link
Author

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.

Copy link

@romulocollopy romulocollopy left a 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

@parkerhancock
Copy link
Contributor

Also fixed in #784

@parkerhancock
Copy link
Contributor

I suggest we close this one in view of #784

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

Successfully merging this pull request may close these issues.

3 participants