为什么我的 Django Post 请求发送错误地解析了我的词典列表?
Why Is My Django Post Request Sending Incorrectly Parsed My List of Dictionaries?
这是我的 Django 视图:
def sendMail(request):
url = 'https://example.com/example'
dictwithlist = request.FILES['dictwithlist']
parsed = json.load(dictwithlist)
// the log file shows that the lists are intact after json.loads
logip(request, json.loads(parsed))
x = requests.post(url, json.loads(clockoutJSON))
return HttpResponse(status=204)
如果我只是发送已解析的数据,我的快速服务器会收到一个空字典 {}。当我记录 json.loads(parsed) 时,我找到了很好的数据,列表完好无损。但是,当数据到达另一侧时,嵌套列表中的字典将全部删除,仅由其键的字符串替换。
我尝试按照此处所述使用 headers: 但我只收到 500 个错误。我不知道我是否格式化 headers 错误。 (因为代码有行距,我在复制)
谁能帮我理解为什么会失败?我需要那个列表来完成它的字典。
我相信您在发送请求时可能需要使用 dumps
而不是 loads
:
x = requests.post(url, json.dumps(parsed))
或者,如果您使用 python requests
库,您可以将 json 作为字典发送,如下所示:
response = requests.post(url=url, json=parsed)
这是我的 Django 视图:
def sendMail(request):
url = 'https://example.com/example'
dictwithlist = request.FILES['dictwithlist']
parsed = json.load(dictwithlist)
// the log file shows that the lists are intact after json.loads
logip(request, json.loads(parsed))
x = requests.post(url, json.loads(clockoutJSON))
return HttpResponse(status=204)
如果我只是发送已解析的数据,我的快速服务器会收到一个空字典 {}。当我记录 json.loads(parsed) 时,我找到了很好的数据,列表完好无损。但是,当数据到达另一侧时,嵌套列表中的字典将全部删除,仅由其键的字符串替换。
我尝试按照此处所述使用 headers:
谁能帮我理解为什么会失败?我需要那个列表来完成它的字典。
我相信您在发送请求时可能需要使用 dumps
而不是 loads
:
x = requests.post(url, json.dumps(parsed))
或者,如果您使用 python requests
库,您可以将 json 作为字典发送,如下所示:
response = requests.post(url=url, json=parsed)