【发布时间】:2014-03-31 19:15:48
【问题描述】:
我正在尝试使用 PowerShell 3 和 4 中的 Invoke-RestMethod cmdlet,通过 REST API 的多部分/表单数据上传来上传大型二进制文件。这是一个关于如何在 PowerShell 中执行我想执行的操作的 cURL 示例:
curl -i -k -H "accept: application/json" -H "content-type: multipart/form-data" -H "accept-language: en-us" -H "auth: tokenid" -F file="@Z:\large_binary_file.bin" -X POST "https://server/rest/uri2"
我希望看到一个关于如何使用 Invoke-RestMethod 发布多部分/表单数据的工作示例。我找到了一个blog post from the PowerShell team showing how to use Invoke-RestMethod 来上传到 OneDrive(又名 SkyDrive),但效果不佳。如果可能的话,我还想避免使用 System.Net.WebClient。我还找到了another thread here on Stackoverflow,但确实没有多大帮助。
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$server = "https://server"
uri = "/rest/uri1"
$headers = @{"accept" = "application/json"; "content-type" = "application/json";"accept-language" = "en-us"}
$body = @{"userName" = "administrator"; "password" = "password"}
$method = "POST"
#Get Session ID
$resp = Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body (convertto-json $Body -depth 99)
$sessionID = $resp.sessionID
#Upload file
$uri = "/rest/uri2"
$headers = @{"accept" = "application/json";"content-type" = "multipart/form-data"; "accept- language" = "en-us"; "auth" = $sessionID}
$medthod = "POST"
$largeFile = "Z:\large_binary_file.bin"
我已经尝试了两种使用 Invoke-RestMethod 的方法:
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -InFile $largeFile
或
$body = "file=$(get-content $updateFile -Enc Byte -raw)"
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body $body
【问题讨论】:
-
我今天在这里添加了一个答案:stackoverflow.com/questions/25075010/…(哦,请更正标题以获得更好的点击重置->休息)