Multi/form-data文件上传形式的post请求:用files传参
fiddler里请求响应内容;
代码:
import requests from requests_toolbelt import MultipartEncoder import json #登录 #form表单形式的post请求,用data传参,Content-Type: application/x-www-form-urlencoded payload={"username":"admin","password":"admin","validateCode":"","rememberMe":"false"} s=requests.session() response3=s.post("http://www.luckyframe.cn/login",data=payload) #上传文件 #上传文件的post请求,用files传参文件,有表单数据用data传参 data1={'clientIp':'127.0.0.1','driverPath':'//TestDriven'} files={'drivenfile':('testng-6.11.jar',open(r'testng-6.11.jar','rb'),'application/octet-stream')} response7=s.post("http://www.luckyframe.cn/testexecution/taskScheduling/uploadJar",files=files,data=data1) print('上传请求的返回结果:',response7.text)
运行结果:
D:Python37python.exe E:/PycharmProjects/home_UI/apistudy/uploadfile.py 上传请求的返回结果: {"msg":"上传驱动包【testng-6.11.jar】到客户端驱动目录【F:\BaiduNetdiskDownload\LuckyFrameClient\classes\TestDriven\testng-6.11.jar】成功!","code":0}