每天努力一点点,坚持下去 ------ 博客首页

接口请求上传文件模块: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/


 

posted @ 2022-09-15 16:37  他还在坚持嘛  阅读(415)  评论(0)    收藏  举报