user2340939
user2340939

Reputation: 1991

POST request with nested JSON parameters malformed in request.POST

Inside integration tests, I try to make a POST request with a nested JSON parameter (this is just one of the approaches I tried):

test_function(admin_client):
    admin_client.post(some_url,
                      json.dumps(some_nested_json),
                      content_type='application/json',
                      headers={'Content-type': 'application/json; charset=utf-8'})

I've also tried all sorts of different combinations for the parameters etc., everything I could find on the web, but I can't get the proper JSON output in the received request.JSON.

I get one of these three cases:

What am I missing here? I'm using Python 2.7.

Upvotes: 0

Views: 503

Answers (1)

Crystal
Crystal

Reputation: 364

This is expected behavior in django. Json requests are not in in request.POST but in request.body. You need to manually do json.loads(request.body) in your view.

Upvotes: 1

Related Questions