[Django] #29420: Single quote in json bytes from RequestFactory

9 views
Skip to first unread message

Django

unread,
May 20, 2018, 1:10:48 AM5/20/18
to django-...@googlegroups.com
#29420: Single quote in json bytes from RequestFactory
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
dunatotatos |
Type: Bug | Status: new
Component: Testing | Version: 2.0
framework | Keywords: requestfactory,
Severity: Normal | json
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi all,

If we use RequestFactory to build a JSON request, the body of the request
is built with single quotes instead of double-quotes. However, According
to [http://www.ecma-international.org/publications/files/ECMA-
ST/ECMA-404.pdf ECMA-404], chapter 9-String:
A string is a sequence of Unicode code points wrapped with quotation
marks (U+0022).

Using the standard `json` library to load this payload raises an
Exception.

Here is a MVP to run in a Django console:

{{{
>>> import json
>>> from django.test import RequestFactory
>>>
>>> data = {'key': 'value'}
>>> request = RequestFactory().post('/', data,
content_type='application/json')
>>> request.body # Here, we see the JSON has been encoded with single
quotes.
b"{'key': 'value'}"
>>> json.loads(request.body) # And json.loads refuses the single quote
Traceback (most recent call last):
[...]
json.decoder.JSONDecoder: Expecting property name enclosed in double
quotes: line 1 column 2 (char 1)
}}}

If this is an expected behavior, it should probably be documented
somewhere in
[https://docs.djangoproject.com/en/2.0/topics/testing/advanced/#the-
request-factory this page], as well as how to POST a JSON request. Except
if I miss something, of course.

--
Ticket URL: <https://code.djangoproject.com/ticket/29420>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 20, 2018, 9:47:53 AM5/20/18
to django-...@googlegroups.com
#29420: Single quote in json bytes from RequestFactory
-------------------------------------+-------------------------------------
Reporter: Dunatotatos | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 2.0
Severity: Normal | Resolution: duplicate
Keywords: requestfactory, | Triage Stage:
json | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* status: new => closed
* resolution: => duplicate


Comment:

Should be fixed in Django 2.1 (see #29082).

--
Ticket URL: <https://code.djangoproject.com/ticket/29420#comment:1>

Reply all
Reply to author
Forward
0 new messages