Opened 6 years ago
Closed 6 years ago
#29420 closed Bug (duplicate)
Single quote in json bytes from RequestFactory
Reported by: | Dunatotatos | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | 2.0 |
Severity: | Normal | Keywords: | requestfactory, json |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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 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 this page, as well as how to POST a JSON request. Except if I miss something, of course.
Should be fixed in Django 2.1 (see #29082).