REST API文件上载-将Python移植到PowerShell

REST API文件上载-将Python移植到PowerShell,python,powershell,Python,Powershell,有人能帮我在PowerShell中执行以下Python过程吗 json_res = json.loads(res.text,object_pairs_hook=collections.OrderedDict) files = {'file':open(image_path,'rb').read()} _data = json_res.items() _data[1] = ('upload_params',_data[1][1].items()) upload_file_response = re

有人能帮我在PowerShell中执行以下Python过程吗

json_res = json.loads(res.text,object_pairs_hook=collections.OrderedDict)
files = {'file':open(image_path,'rb').read()}
_data = json_res.items()
_data[1] = ('upload_params',_data[1][1].items())
upload_file_response = requests.post(json_res['upload_url'],data=_data[1][1],files=files,allow_redirects=False)
下面是我的PowerShell,它使用以下命令获取json_res对象中使用的初始json响应:

$initialization_parameters = @{
  "name" = $($image.Name);
  "size" = $($image.Length);
  "content_type" = $(if ($image.Extension -match 'jp*g') { "image/jpeg" } elseif ($image.Extension -match 'png') { "image/png" } elseif ($image.Extension -match 'gif') { "image/gif" } else { "image/jpeg" });
  "parent_folder_path" = "profile pictures";
  "as_user_id" = $("sis_user_id:" + $file_id) 
}

$initialization_parameters = $initialization_parameters | ConvertTo-Json

$initial_response = Invoke-RestMethod -Uri $avatars_base_url -Headers $headers -Body $initialization_parameters -ContentType "application/json" -Method POST
我不知道如何做下一个请求,虽然并添加上传图像-我已经尝试发送一个字节数组或只是本地文件路径,但这些似乎不起作用

具体来说,我想知道如何最好地模仿PowerShell中Python代码的第2行和第5行


任何帮助都将不胜感激。

我想这就是你需要做的。如果没有,希望它至少能让你走上正轨

$FileContent = [System.IO.File]::ReadAllBytes($Image.FullName)
$UploadInfo @{
    "File" = $Image.Name
    "Data" = [System.Convert]::ToBase64String($FileContent)
}
$FileUpload = Invoke-JSONMethod -uri $Upload_URL -ContentType "application/json" -Method POST -Body (ConvertTo-Json $UploadInfo) -AcceptType "application/json"

我认为这样做不行-我认为我需要使用多部分方法,因为我总是在错误(412)的前提条件下失败。不幸的是,我不知道如何在PowerShell中使用多部分方法。