[Python] Skip utf8 decoding for specific Content-Types in py3
Created by: g-bon
In rest.py if PY3 is used and _preload_content is True, the response content is always decoded as utf8. This doesn't make sense for non-text files, e.g. an api that returns a file.
if six.PY3:
r.data = r.data.decode('utf8')
I welcome any suggestion on how to tackle this but the two solution that come to mind are:
- Check on the content_type and make a whitelist/blacklist of content types that should be decoded to utf8.
A simple example would be something like
if six.PY3 and headers.get('Content-Type') not in ['application/octet-stream']:
r.data = r.data.decode('utf8')
Where obviously the list of Content-Types to "skip" would be a meaningful one and not only octet-stream.
- Another, harder approach would be to infer the content type from the actual data but feels fragile and overkill to me. Positive side would be that it would likely do the right thing even if the wrong or no content-type is specified in the response headers.
Happy to work on this when I have some confirmation the problem is here / feedback on the right approach.