基于Python3的接口自动化总结(一)——文件上传接口

2023-11-18

文件上传接口

api: POST http://host/api/import
接口入参:file:文件内容

import os
import urllib3
import ast


def import_by_rest(self, file_path):
    """
    :param  :file_path:导入文件路径
    :return :res:接口返回数据
    """
    api = "http://host/api/import"
    file_name = os.path.basename(file_path)
    fields = {"file": (file_name, open(file_path, "rb").read())}
    headers = {"charset": "utf-8"}
    headers["Content-Type"] = "multipart/form-data; boundary=------WebKitFormBoundaryFJ1qMeiyjBLQBNas"
    response = urllib3.PoolManager().request("post", api, fields=fields, headers=headers, multipart_boundary="------WebKitFormBoundaryFJ1qMeiyjBLQBNas")
    res = ast.literal_eval(response.data.decode('utf-8'))
    assert response.status is 200, res
    return res
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于Python3的接口自动化总结(一)——文件上传接口 的相关文章

随机推荐