Powershell 调用RestMethod:必须将ContentLength字节写入请求流

Powershell 调用RestMethod:必须将ContentLength字节写入请求流,powershell,powershell-3.0,Powershell,Powershell 3.0,我正在尝试将文件上载到URL。我尝试了这两种方法: Invoke-RestMethod -Uri $uploadUrl -Method Put -Headers $uploadHdrs -InFile $uploadFilePath Invoke-RestMethod -Uri $uploadUrl -Method Put -body $uploadFileBody -Headers $uploadHdrs 我得到的错误是: Invoke-RestMethod : You must writ

我正在尝试将文件上载到URL。我尝试了这两种方法:

Invoke-RestMethod -Uri $uploadUrl -Method Put -Headers $uploadHdrs -InFile $uploadFilePath

Invoke-RestMethod -Uri $uploadUrl -Method Put -body $uploadFileBody -Headers $uploadHdrs
我得到的错误是:

Invoke-RestMethod : You must write ContentLength bytes to the request stream
before calling [Begin]GetResponse.
如果我添加
-transferncoding
参数,我会从服务器收到错误,说不支持


我能做些什么来包含内容长度?

我认为您需要使用不同的命令,比如调用WebRequest,或者更好地直接从PowerShell调用.NET或方法

虽然REST方法在概念上可能包括上载文件,但这并不意味着Invoke WebRequest命令已被测试为支持您的场景。我建议降低级别(比如WebClient),因为我们知道更多的场景已经过测试(由更大的.NET团队测试),而且WebClient上有各种各样的方法来支持特定场景

关于如何从PowerShell调用这些方法,您可能会发现一个很有帮助的示例


另外,您得到的关于请求流的消息来自更低级别的.NET API,但WebClient使用更简单,应该注意正确设置ContentLength,希望它“正好”满足您的需要。

$uploadHdrs
?content-md5头中有什么内容。如果排除头参数,则会出现相同的错误。请显示
$uploadFileBody
的内容。如果是原始文件,那么这可能会有所帮助:答案是使用infle。正如在问题中一样,这也有同样的问题。使用infle$uploadFilePath是一个文件路径,使用body参数,它是[System.IO.File]::ReadAllBytes(“路径到文件”),您是否尝试过
$uploadFileBody=Get Content$uploadFilePath