TypeError: Response.body being set to a text object

Issue #5 resolved
Ankush Chadda
created an issue

Im using WebOb==1.2.3.

I throws error TypeError: You cannot set Response.body to a text object (use Response.text)

Possible solution could be as given in docs


Comments (8)

  1. Jim Thaxton

    I received a similar error and this is the traceback information. I get this when I click any TRACE link.

    Traceback (most recent call last):
      File "/var/www/t/env/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 283, in run
        self.result = application(self.environ, self.start_response)
      File "/usr/lib/python2.7/site-packages/dozer/leak.py", line 97, in __call__
        return self.dowse(req)(environ, start_response)
      File "/usr/lib/python2.7/site-packages/dozer/leak.py", line 108, in dowse
        return method(req)
      File "/usr/lib/python2.7/site-packages/dozer/leak.py", line 220, in trace
        objid=str(objid or ''))
      File "/usr/lib/python2.7/site-packages/webob/response.py", line 373, in _body__set
        raise TypeError(msg)
    TypeError: You cannot set Response.body to a text object (use Response.text)
  2. Mitchell Peabody

    pip installs WebOb 1.2.3 as a supporting package for Dozer. So, I was using WebOb 1.2.3 when I sent you the pull request. It also appears that there are other places in the source where this switch from Response.body to Response.text is needed (the tree function for example).

    Looking a bit closer at some of the WebOb code, what's going on is the the default behavior for Response.init is to set the content type to utf-8. When the template (in leak.py) function returns, the type is sometimes unicode and sometimes str. You can verify this by comparing the type returned from applying the template function to graphs.html (Dozer.index) and the type return from applying the template function to trace.html (Dozer.trace).

    The set method for Response.body detects if the content-type is binary or not and if it isn't, checks what the type the new value is and raises the TypeError if it's of a "text_type" which is defined in compat.py to be unicode for python versions < 3.

    Right now, template is returning both str and unicode responses. When it's an str return, then setting Response.body works, but not Response.text. When it's a unicode return, then setting Response.text works, but not Response.body. So either there needs to be consistency in the return of the template function, a check needs to be made to set the appropriate response field, or a conversion to unicode when Response.text is set.

  3. Log in to comment