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