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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/054.0784e108940b81657bc8d12266572637%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to