您好,登錄后才能下訂單哦!
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è)參考,也希望大家多多支持億速云。
免責(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)容。