python requests post多層字典的方法
pyhton requests模塊post方法傳參為多層字典時,轉(zhuǎn)換錯誤,
如,表單傳參
{ “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ù)會被自動轉(zhuǎn)化為form-encoded,查看請求后,這里的demo是一個一層的字典。多層字典傳輸?shù)臅r候,確實會異常。
解決辦法:
將第二層字典轉(zhuǎn)化為json,json.dumps({“A”:2,”B”:3}),然后賦給b。
以上這篇python requests post多層字典的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
django執(zhí)行數(shù)據(jù)庫查詢之后實現(xiàn)返回的結(jié)果集轉(zhuǎn)json
這篇文章主要介紹了django執(zhí)行數(shù)據(jù)庫查詢之后實現(xiàn)返回的結(jié)果集轉(zhuǎn)json,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Python中Pandas庫的數(shù)據(jù)處理與分析
Python的Pandas庫是數(shù)據(jù)科學領(lǐng)域中非常重要的一個庫,它使數(shù)據(jù)清洗和分析工作變得更快更簡單,Pandas結(jié)合了NumPy的高性能數(shù)組計算功能以及電子表格和關(guān)系型數(shù)據(jù)庫(如SQL)的靈活數(shù)據(jù)處理能力,需要的朋友可以參考下2023-07-07淺析Python中正則表達式函數(shù)search()和match()的使用
在Python中,正則表達式是處理字符串的強大工具,search()和match()是Python標準庫中re模塊中兩個常用的正則表達式方法,本文將詳細講解這兩個方法的使用,需要的可以參考一下2023-08-08