接口请求上传文件模块:requests_toolbelt
我们在接口请求中,遇到需上传文件时,可使用requests_toolbelt
安装
pip install requests_toolbelt
代码如下:
from requests_toolbelt.multipart.encoder import MultipartEncoder req_url = env_dict['host'] + data_yaml['interface'] filePath = yaml_case['body']['file'] m = MultipartEncoder( fields={ 'file': ('netOrderTest.xls', open(filePath, 'rb'), 'application/vnd.ms-excel'), 'entCertNo': yaml_case['body']['entCertNo'] }) # 拼接headers数据 headers['Content-Type'] = m.content_type results = requests.post(req_url, data=m, headers=headers, verify=False) print(json.dumps(results.json(), ensure_ascii=False))
官网:https://pypi.org/project/requests-toolbelt/
本文来自博客园,作者:他还在坚持嘛,转载请注明原文链接:他还在坚持嘛 https://www.cnblogs.com/brf-test/p/16697053.html