[英]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.