﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29082	Make the test client automatically encode JSON data	Nick Sarbicki	nobody	"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."	Cleanup/optimization	closed	Testing framework	2.0	Normal	fixed		Adam Johnson	Ready for checkin	1	0	0	0	0	0
