简体   繁体   中英

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:

  • request.POST contains the first level of JSON plus the arrays of second level JSON keys
  • request.POST contains the first level of JSON plus an empty second level JSON
  • request.POST is empty, but request.body contains the whole nested JSON, in expected format

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM