Thursday, March 31, 2016

Re: Test Client response.json() UnicodeDecodeError

Looks reasonable, although I am not entirely clear what role charset has in a JsonResponse. See https://code.djangoproject.com/ticket/23949.

On Thursday, March 31, 2016 at 6:15:53 AM UTC-4, Daniel Blasco wrote:
Hi,

I'm testing the REST API of my application and I found an error that I think that's a Django's issue.

I'm using:
  • Django==1.9.4
  • djangorestframework==3.3.3

I have set the rest framework setting UNICODE_JSON to True.

When i'm testing a JSON response that contains a special character like 'á' then I get a UnicodeDecodeError. This is the traceback:

Traceback (most recent call last):
  File ".../tests/assets/test_views.py", line 584, in test_get
    response.json()
  File ".../lib/python2.7/site-packages/django/utils/functional.py", line 13, in _curried
    return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
  File ".../lib/python2.7/site-packages/django/test/client.py", line 662, in _parse_json
    return json.loads(response.content.decode(), **extra)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 268: ordinal not in range(128)


If I change the line 662 in django/test/client.py to:

return json.loads(response.content.decode(response.charset), **extra)

then it works fine. In this case response.charset is utf-8.


Thanks,
Daniel

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c3fba296-1bf0-45dc-97e7-c8d33cee310c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment