简体   繁体   English

带有嵌套JSON参数的POST请求在request.POST中格式错误

[英]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): 在集成测试中,我尝试使用嵌套的JSON参数发出POST请求(这只是我尝试的方法之一):

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 . 我还尝试了各种不同的参数等组合,这些都是我可以在网上找到的,但是我无法在收到的request.JSON获得正确的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包含第一层JSON以及第二层JSON 密钥的数组
  • request.POST contains the first level of JSON plus an empty second level JSON request.POST包含第一级JSON和一个空的第二级JSON
  • request.POST is empty, but request.body contains the whole nested JSON, in expected format request.POST为空,但request.body包含整个嵌套的JSON,格式为预期

What am I missing here? 我在这里想念什么? I'm using Python 2.7 . 我正在使用Python 2.7

This is expected behavior in django. 这是django中的预期行为。 Json requests are not in in request.POST but in request.body . Json请求不在request.POST而在request.body You need to manually do json.loads(request.body) in your view. 您需要在视图中手动执行json.loads(request.body)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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