当前位置: 移动技术网 > IT编程>脚本编程>Python > python requests post多层字典的方法

python requests post多层字典的方法

2019年01月07日  | 移动技术网IT编程  | 我要评论

辣椒种植技术,批发假发,美国海军暴菊聚会

pyhton requests模块post方法传参为多层字典时,转换错误,

如,表单传参

{ 
“a”:1, 
“b”:{ 
“a”:2, 
“b”:3 
} 
}

post请求后看到form data是:

a:1 
b:a 
b:b

查看官方文档:

more complicated post requests
typically, you want to send some form-encoded data — much like an html form. to do this, simply pass a dictionary to the data argument. your dictionary of data will automatically be form-encoded when the request is made:

payload = {‘key1': ‘value1', ‘key2': ‘value2'}

r = requests.post(“http://httpbin.org/post“, data=payload) 
print(r.text) 
{ 
… 
“form”: { 
“key2”: “value2”, 
“key1”: “value1” 
}, 
… 
}

这里说post传入的dict类型参数会被自动转化为form-encoded,查看请求后,这里的demo是一个一层的字典。多层字典传输的时候,确实会异常。

解决办法:

将第二层字典转化为json,json.dumps({“a”:2,”b”:3}),然后赋给b。

以上这篇python requests post多层字典的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网