#29082: force_bytes from utils.encoding.py produces invalid JSON for RequestFactory --------------------------------------+------------------------ Reporter: Nick | Owner: nobody Type: Bug | Status: new Component: Utilities | Version: 2.0 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | --------------------------------------+------------------------ I stumbled on this while testing some code which expects a JSON body.
When creating a RequestFactory post request with a dict for the data and content_type='application/json' the resultant body is invalid JSON. This is because force_bytes simply detects it is not a string and forces it to a string before encoding. Code to reproduce is here: {{{ In [1]: from django import test In [2]: factory = test.RequestFactory() In [3]: req = factory.post('/', content_type='application/json', data={"test": "data"}) In [4]: req.body Out[4]: b"{'test': 'data'}" }}} And the offending code is here: https://github.com/django/django/blob/master/django/utils/encoding.py#L103 Assuming this is not an intended "feature" I will happily submit a patch adding _another_ isinstance line checking for dicts and encoding with json.dumps. -- Ticket URL: <https://code.djangoproject.com/ticket/29082> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.e05fea98f0771ee51e22816f17c734b9%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.