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多層字典的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答