溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python requests post多層字典的方法

發(fā)布時(shí)間:2020-09-13 21:04:38 來源:腳本之家 閱讀:269 作者:monkey7777 欄目:開發(fā)技術(shù)

pyhton requests模塊post方法傳參為多層字典時(shí),轉(zhuǎn)換錯(cuò)誤,

如,表單傳參

{ 
“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類型參數(shù)會(huì)被自動(dòng)轉(zhuǎn)化為form-encoded,查看請求后,這里的demo是一個(gè)一層的字典。多層字典傳輸?shù)臅r(shí)候,確實(shí)會(huì)異常。

解決辦法:

將第二層字典轉(zhuǎn)化為json,json.dumps({“A”:2,”B”:3}),然后賦給b。

以上這篇python requests post多層字典的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI