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.
* 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>