Search code examples
pythonpython-3.xwsgiwebob

Return binary data by webob.Response


I`m trying to return binary in webob.Response but error happens

file = open(filePath, 'rb')
buffer = file.read()
file.close()

self._response.body = buffer

Gives the traceback

"F:\Program Files\Python32\python.exe" G:/development/projects/AutoBlog/main.py
Traceback (most recent call last):
File "F:\Program Files\Python32\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
File "G:/development/projects/AutoBlog/main.py", line 30, in application
    print(wsgiApplication)
File "F:\Program Files\Python32\lib\site-packages\webob\response.py", line 218, in __str__
    parts += ['', self.text if PY3 else self.body]
File "F:\Program Files\Python32\lib\site-packages\webob\response.py", line 383, in _text__get
    return body.decode(self.charset, self.unicode_errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
activate.adobe.com - - [20/May/2012 14:52:49] "GET /res/images/i.jpg HTTP/1.1" 500 59

Solution

  • Looks like webob is treating your body as a string, whereas it is actually a binary object. Maybe you need to set the content type?