1. 程式人生 > 其它 >requests 上傳檔案

requests 上傳檔案

@seldom.file_data("test_data/device/device.yaml", key="test_importDevice")
@seldom.skip("ok")
def test_importDevice(self, file_path, code, device_name):
    """[裝置匯入]
    """
    url = "/maicloud-manager/device/v1/importDevice"

    body = {}
    json_data = json.dumps(body)
    param_len = len(json_data)
    data_base64 = base64.b64encode(json_data.encode())
    url = "https://11.0.54.20:443/api/maicloud-manager/device/v1/importDevice"
    data = {
        "X-Access-Token": self.browser_user.X_Access_Token,
        "X-Param-Length": param_len,
        "body": data_base64.decode("utf-8")
    }

    file_name = os.path.basename(file_path)
    file = {'file': (file_name, open(file_path, "rb"),
                        "application/octet-stream")}
    # 'file' 上傳檔案的鍵名 
    # file_name 上傳到伺服器的檔名,可以和上傳的檔名不同
    # open(file_path, "rb")開啟的檔案物件,注意檔案路徑正確
    # "application/octet-stream" Content-Type型別
    self.post(url, data=data, files=file,  verify=False)
    self.assertPath("code", code)