接口自动化-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