Test Client response.json() UnicodeDecodeError

215 views
Skip to first unread message

Daniel Blasco

unread,
Mar 31, 2016, 6:15:53 AM3/31/16
to Django users
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

Tim Graham

unread,
Mar 31, 2016, 8:24:39 AM3/31/16
to Django users
Looks reasonable, although I am not entirely clear what role charset has in a JsonResponse. See https://code.djangoproject.com/ticket/23949.

Aniruddha Maru

unread,
Mar 2, 2017, 5:59:46 PM3/2/17
to Django users
Running into the same issue - charset isn't correct, I think it ought to be hardcoded to `utf-8`.

Aniruddha Maru

unread,
Mar 2, 2017, 6:02:02 PM3/2/17
to Django users
Reply all
Reply to author
Forward
0 new messages