pyhton requests模块post方法传参为多层字典时,转换错误,
如,表单传参
1
2
3
4
5
6
7
|
{ “a”: 1 , “b”:{ “A”: 2 , “B”: 3 } } |
post请求后看到form data是:
1
2
3
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
|
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多层字典的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/monkey7777/article/details/75109962