接口測試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