我目前正在尝试使用 REST API 将文件上传到网络服务器。正如前面提到的,我正在使用 PowerShell 来实现此目的。使用curl,这没有问题。调用看起来像这样:
curl -H "Auth_token:"$AUTH_TOKEN -H "Content-Type:multipart/form-data" -X POST -F appInfo='{"name": "test","description": "test"}' -F uploadFile=@/test/test.test https://server/api/
但当涉及到使用 Invoke-Restmethod 命令将其导出到 powershell 时,我完全无能为力。据我搜索,不可能使用 Invoke-Rest 方法来实现此目的。https://www.snip2code.com/Snippet/396726/PowerShell-V3-Multipart-formdata-example但即使有了这个剪裁,我也不够聪明,无法完成这项工作,因为我不想上传两个文件,而是上传一个文件和一些参数。
如果有人能让我重回正轨,我将非常感激:o
谢谢!
@Bacon-Bits 的答案似乎对我不起作用。我的服务器以可能格式错误的表单数据正文拒绝了它:-(
I found 这个要点,并根据我的目的对其进行了一些修剪。这是我的最终结果:
$FilePath = 'c:\temp\temp.txt';
$URL = 'http://your.url.here';
$fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString();
$LF = "`r`n";
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"temp.txt`"",
"Content-Type: application/octet-stream$LF",
$fileEnc,
"--$boundary--$LF"
) -join $LF
Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)