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。