gpt4 book ai didi

python - 使用 Python lib 请求发送列表

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:05 25 4
gpt4 key购买 nike

我有一个包含列表的 python 字典,例如:

d = {"x": 1, "y": ["a", "b", "c"]}

我想使用 requests lib 从我的 python 项目中通过 POST 发送此字典,如下所示:

requests.post(url, d, verify=False)

我设法很好地发送和接收请求,但问题是当我在 Django View 中打印 request.POST 字典时,它看起来像这样:

{"y": "c", "x": "1"}

包含列表的 y 键看起来并不像预期的那样,因为列表已缩减为列表的最后一个对象,在本例中为 "c" 。如何正确发送列表?

最佳答案

您需要使用QueryDict.getlist() method检索请求中重复的参数的所有值:

y = request.POST.getlist('y')

这假设您改为发布 d = [('x', 1), ('y', 'a'), ('y', 'b'), ('y', 'c ')]; 请求不支持嵌套列表。

关于python - 使用 Python lib 请求发送列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20946296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com