接口自動化-Python3+request上傳文件,發送multipart/form-data編碼


1、安裝requests_toolbelt  

pip install requests-toolbelt

2、發送文件中的數據

from requests_toolbelt import MultipartEncoder

from kyeapi.common.httprequests import Requests (自己封裝了request模塊)

def upload_file(file_path):
# 文件名稱
file_name = os.path.basename(file_path)
# 獲取文件類型
mime_type = filetype.guess(file_path).mime
# 讀取文件內容
with open(file_path, 'rb') as f:
file_handler = f.read()

# 請求的參數(包含了文件)
fields = {'bizCode':'oams_business_reception_screen_biz_code','bizId':'p526a38uyl205cozehtv',
'token':tokens('uat'),'x-uid':'151698280650944976',"file": (file_name, file_handler, mime_type)} #紅色部分根據各自上傳附件時是否需求另外的參數
   # 將請求的參數轉換成 MultipartEncoder格式

encode_data = MultipartEncoder(fields)
return encode_data


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM