使用Invoke-WebRequest或Invoke-RestMethod通过Powershell 7上传多个文件

时间:2020-06-10 18:08:42

标签: powershell curl invoke-webrequest powershell-7.0

我希望使用Invoke-WebRequest或Invoke-RestMethod等效于以下cURL命令,以便我可以轻松解析响应(确切的文件并不重要)。

curl -X POST "https://example.com" --max-time 20 -F "files[]=@foo.json;filename=foo.json" -F "files[]=@foo/bar.xml;filename=foo/bar.xml"

Invoke-WebRequest的文档指出:

如果存在System.IO.FileInfo值,则提交文件内容。如果存在诸如数组或列表之类的集合,则表单字段将被多次提交。

因此,我想出了以下代码段。

$files = Get-ChildItem -File -Recurse
$form = @{
    files = $files
}
try {
    $response = Invoke-RestMethod -Method Post -Uri "https://example.com" -TimeoutSec 20 -Form $form
    # successful response
} catch {
    Write-Host $_
}

但是,由于服务器正在发送以下响应,因此请求似乎并不等效,甚至无效。

找不到多部分文件

0 个答案:

没有答案