You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 30, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 30, 2023. It is now read-only.
Response.write() tries to decode bytes from binary stream #146
I'm trying to stream a file to the web browser, and to do this I'm using handler.request.write(bytes). However webapp tries to convert the bytes to text, which fails as the bytes are not valid for any character set:
text = text.decode(self.default_charset)```
This results in an error:
`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 6: invalid continuation byte`
In a python2 environment writing bytes was supported.
I've tried modifying the Response.write() function to do no processing if bytes are passed in, and this allows me to stream a .gif to the browser successfully.
I'm running in to issues with a gzip encoded SVG, although this is more an issue of application logic setting the headers correctly (or decompressing the SVG before streaming to the browser). Once I've been able to test this I think I can craft a PR to resolve this issue.
FYI: I ran into this issue as well, and was able to work around it by bypassing the subclass's Response.write method and called directly into the base class's write method.
It occurs when trying to pass bytes content (e.g. a binary asset like a JPG file) as the text parameter.
I worked around it by replacing the self.response.out.write(...) code in my handler with the following, where I bypass the subclass's write method and just call into the webob.Response.write directly.
fp = open(path, 'rb') # Path is the path to the binary file I'm trying to send.
super(webapp2.Response, self.response).write(fp.read())
FYI: This seems to work for me, but I'm not sure if there are any side effects that may crop up from this approach.
Activity
jeremydw commentedon Aug 24, 2020
FYI: I ran into this issue as well, and was able to work around it by bypassing the subclass's
Response.writemethod and called directly into the base class'swritemethod.The error happens here:
https://github.com/GoogleCloudPlatform/webapp2/blob/master/webapp2.py#L420
It occurs when trying to pass
bytescontent (e.g. a binary asset like a JPG file) as thetextparameter.I worked around it by replacing the
self.response.out.write(...)code in my handler with the following, where I bypass the subclass'swritemethod and just call into thewebob.Response.writedirectly.FYI: This seems to work for me, but I'm not sure if there are any side effects that may crop up from this approach.