python如何使用requests提交post請求并上傳文件(multipart/form-data)
一、背景
也是前幾天,有一個需求上傳文件需要自動化。具體是上傳到系統(tǒng)一個文件,并收到返回結(jié)果??紤]使用python的requests,一般這種查詢或上傳文件的接口都是post請求。所以就直接使用requests的post請求。但是在開發(fā)過程中,遇到一些問題需要注意。所以在此記錄一下。
二、請求接口上傳文件
2.1、分析接口
首先瀏覽器f12查看接口內(nèi)容(主要看接口類型、請求頭、Payload)。發(fā)現(xiàn)上傳文件的接口是post類型,請求頭中Content-Type也很重要,指定內(nèi)容類型及請求體的一個分隔符。詳見下圖。
Payload里是接口的請求體,詳見下圖。接口參數(shù):type、orgType、file ,分別對應(yīng)下圖。其中,file的值為上傳的文件(轉(zhuǎn)換為二進制數(shù)據(jù))
對應(yīng)參數(shù)的請求內(nèi)容,其中------WebKitFormBoundary5rEpBecoRZ2tj60k為分割符,每個兩個分割符之前對于一個參數(shù)。
2.2、python進行請求
# 請求頭 ''' 這里注意,要將Content-Type注釋掉。因為在請求的時候,會自動加上。 ''' header = { 'Authorization': '1677034306556', 'Connection': 'keep-alive', # 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryFXTT4S1LKA1LUDBd', 'Cookie': 'SHIROJSESSIONID=75ace860-0f00-4db0-9440-6c6d53cdf101', 'Host': 'host:8088', 'Origin': 'http://host:8088', 'Referer': 'http://host:8088/njfxq/search/clue/clueFeedBackDetailAll?id=1574192996457648130&Paramspage=clue&caseId=1567439544410976257', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36' } # 請求體Payload ''' 這里有必要解釋下: 如果請求體按照頁面顯示的配置如下: fileObject = { 'type':'6', 'orgType': 'B', 'file': open('上傳文件.xlsx','rb') } 是錯誤的(第一次花費半天才調(diào)通) // 正確的格式應(yīng)該是傳入一個元組,格式為:(<fileName>,<fileObject>,<Content-Type>) ,這里的fileObject是指具體的值。 正確的請求體應(yīng)為: fileObject = { 'type':(None,'6',None), 'orgType': (None,'B',None), 'file': ('上傳文件.xlsx',open('上傳文件.xlsx','rb'),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') } ''' fileObject = { 'type':(None,'6',None), 'orgType': (None,'B',None), 'file': ('上傳文件.xlsx',open('上傳文件.xlsx','rb'),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') } req = requests.post('http://host:8088/njfxq/finance/investigatefeedback/uploadFile',headers=header,files=fileObject) print(req.text)
三、總結(jié)
Payload請求體如何轉(zhuǎn)換的問題,看下圖應(yīng)該比較容易理解。
# 下面為補充后的Payload ------WebKitFormBoundarynS4EDa2hdT8tfnF8 Content-Disposition: form-data; name="type"; filename=None content-type: None fileObject ------WebKitFormBoundarynS4EDa2hdT8tfnF8 Content-Disposition: form-data; name="orgType"; filename=None content-type: None fileObject ------WebKitFormBoundarynS4EDa2hdT8tfnF8 Content-Disposition: form-data; name="file"; filename="樣本標簽.xlsx" Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet fileObject # 為文件的二進制數(shù)據(jù) ------WebKitFormBoundarynS4EDa2hdT8tfnF8-- # 轉(zhuǎn)換為python的請求格式 格式為:'name':(<fileName>,<fileObject>,<Content-Type>) # 對比如下 fileObject = { 'type':(None,'6',None), 'orgType': (None,'B',None), 'file': ('上傳文件.xlsx',open('上傳文件.xlsx','rb'),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
總結(jié)
到此這篇關(guān)于python如何使用requests提交post請求并上傳文件的文章就介紹到這了,更多相關(guān)python提交post請求上傳文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python+Splinter實現(xiàn)12306搶票功能
這篇文章主要為大家詳細介紹了python+Splinter實現(xiàn)12306搶票功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09使用Python第三方庫xlrd讀取Excel中的數(shù)據(jù)的流程步驟
這篇文章主要給大家介紹了使用Python第三方庫xlrd讀取Excel中的數(shù)據(jù)的流程步驟,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2023-12-12Python時間戳與時間字符串互相轉(zhuǎn)換實例代碼
這篇文章主要介紹了Python時間戳與時間字符串互相轉(zhuǎn)換實例代碼,大家參考使用2013-11-11