接口测试request上传文件的两种方式


话不多说,直接给案例

请求Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryj0QkLJMnALDH5DDw

1.查看请求为二进制文件的方式

直接用files模拟,每个Content-Disposition,写个字段key-value

 

 2.第二种方式

 url = "http://test.quasar.oa.com:8082/assess/api/AssessObject/ImportStaff"
        headers = {
            "Accept": "application/json, text/plain, */*",
            # content-type参数,如果我们通过form-data的方式上传文件,我们发送post请求的时候,headers这个参数中一定不能要包括这个值,requests库会帮忙添加这个元素,加了可能会报错
            # "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryNZB2Bg7I8L7EropZ",
            "Cookie": config.config.cookie
        }
        path = (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test.xlsx'))
        files = {'file': open(path, 'rb')}
        data={"PackageID":"616503661f42853cf46662ae"}
        res = requests.post(url=url, headers=headers, files=files,data=data)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM