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

Error 400 raises TypeError: expected string or buffer #13

Open
H0neyBadger opened this issue Feb 29, 2016 · 3 comments
Open

Error 400 raises TypeError: expected string or buffer #13

H0neyBadger opened this issue Feb 29, 2016 · 3 comments

Comments

@H0neyBadger
Copy link

Hello,
I often get the following error when my API is supposed to raises an error 400.

2016-02-29 16:25:22,765 [ERROR] django.request.handle_uncaught_exception: Internal Server Error: /api/v1/users/
Traceback (most recent call last):
  File "/home/user/lib/python3.4/site-packages/django/core/handlers/base.py", line 174, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/user/lib/python3.4/site-packages/django/core/handlers/base.py", line 172, in get_response
    response = response.render()
  File "/home/user/lib/python3.4/site-packages/django/template/response.py", line 160, in render
    self.content = self.rendered_content
  File "/home/user/lib/python3.4/site-packages/rest_framework/response.py", line 71, in rendered_content
    ret = renderer.render(self.data, media_type, context)
  File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 37, in render
    self._to_xml(xml, data)
  File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 53, in _to_xml
    self._to_xml(xml, value)
  File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 47, in _to_xml
    self._to_xml(xml, item)
  File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 61, in _to_xml
    xml.characters(smart_text(data))
  File "/home/user/lib/python3.4/site-packages/django/utils/xmlutils.py", line 24, in characters
    if content and re.search(r'[\x00-\x08\x0B-\x0C\x0E-\x1F]', content):
  File "/home/user/lib/python3.4/re.py", line 166, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

In the debug report, I can see the following value:

content = <django.utils.functional.lazy.<locals>.__proxy__ object at 0x7fb564172828>

Did I miss something in my DRF configuration?

djangorestframework-xml==1.3.0
djangorestframework==3.3.2
@kyleobrien91
Copy link
Collaborator

@H0neyBadger - sorry for the super delayed response here. I just want to check in with you if you remember if and how this was resolved on your end and if this has anything to do with django-rest-framework-xml?

@philall
Copy link
Contributor

philall commented Jul 12, 2017

@kyleobrien91 @H0neyBadger - same error here. we use django.utils.translation.ugettext_lazy. ugettext_lazy translates the strings in a lazy fashion return <class 'django.utils.functional.lazy.<locals>.__proxy__'> is a Promise. In the XMLRenderer._to_xml method django-rest-framework-xml use django.utils.encoding.smart_text if we look more closely this method

if isinstance(s, Promise):
    # The input is the result of a gettext_lazy() call.
    return s

So smart_text will never return a string in this case.

(Pdb) type(value)
<class 'django.utils.functional.lazy.<locals>.__proxy__'>
(Pdb) type(smart_text(value))
<class 'django.utils.functional.lazy.<locals>.__proxy__'>
(Pdb) isinstance(value, Promise)
True

Temporary fix: override the _to_xml() method and use django.utils.encoding.force_text instead of django.utils.encoding.smart_text

(Pdb) type(force_text(value))
<class 'str'>

I hope this has answered your question if this has anything to do with django-rest-framework-xml ?

I have already created a fork. But I am now 2 weeks on vacation.

@philall
Copy link
Contributor

philall commented Aug 2, 2017

@jpadilla i think #23 fixed this issue.

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

No branches or pull requests

3 participants