使用PowerShell Invoke-RestMethod来发布大型二进制多部分表单数据

使用PowerShell Invoke-RestMethod来发布大型二进制多部分表单数据,第1张

概述我正在尝试使用Power Shell 3和4中的 Invoke-RestMethod cmdlet,使用REST API的multipart / form-data上传来上传大型二进制文件.这是一个有关如何在PowerShell中执行我想要执行的 *** 作的cURL示例: curl -i -k -H "accept: application/json" -H "content-type: multipa 我正在尝试使用Power Shell 3和4中的 Invoke-RestMethod cmdlet,使用REST API的multipart / form-data上传来上传大型二进制文件.这是一个有关如何在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来POST多部分/表单数据的工作示例.我发现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
解决方法 我知道你在一年前问过并且可能已经解决了这个问题,但是为了其他可能有同样问题的人,我会离开我的回答.

我注意到你的invoke语句中有几个错误.首先,您需要使用POST关键字而不是字符串值.其次,确保将Content-Type设置为multipart / form-data.所以这是我修改后的声明 –

$uri = "http://blahblah.com"$imagePath = "c:/justarandompic.jpg"$upload= Invoke-RestMethod -Uri $uri -Method Post -Infile $imagePath -ContentType 'multipart/form-data'

您可以通过选中$upload来检查服务器的响应.

总结

以上是内存溢出为你收集整理的使用PowerShell Invoke-RestMethod来发布大型二进制多部分/表单数据全部内容,希望文章能够帮你解决使用PowerShell Invoke-RestMethod来发布大型二进制多部分/表单数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1084627.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存