mai*_*ght 8 rest powershell curl
我目前正在尝试使用REST API将文件上传到Web服务器.如上所述,我正在使用PowerShell.卷曲这没问题.电话看起来像这样:
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 但即使有了Snipped,我也不够聪明,因为我不想上传两个文件而是一个文件和一些参数.
如果有人能让我回到赛道上,我将非常感激:哦谢谢!
jkl*_*ack 16
@Bacon-Bits的答案对我来说似乎没有用.我的服务器拒绝了它有一个可能格式错误的表单数据正文:-(
我找到了这个要点,并根据我的目的修剪了一下.这是我的最终结果:
$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
Bac*_*its 12
它应该很直接.从这个回答:
$Uri = 'https://server/api/';
$Headers = @{'Auth_token'=$AUTH_TOKEN};
$FileContent = [IO.File]::ReadAllText('C:\test\test.test');
$Fields = @{'appInfo'='{"name": "test","description": "test"}';'uploadFile'=$FileContent};
Invoke-RestMethod -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Headers $Headers -Body $Fields;
[IO.File]::ReadAllBytes()如果文件不是文本文件,您可能想要使用.
如果您正在上传一个巨大的文件,这也可能无法正常工作.
因此,我最近对此进行了相当多的斗争,发现确实可以匹配curl功能,但如何正确执行多部分/表单数据并不是立即显而易见的。上面的所有回复都涵盖了这个难题的重要部分,但我将尝试将其全部整合在一起,以供下一个尝试在本机 Powershell 中实现curl 功能的抱歉人员使用。
@jklemmack 的解决方案是让我走上正轨的解决方案,并且是最灵活的,因为它允许您专门构建表单数据内容,控制两个边界以及数据在其中的格式化方式。
对于尝试执行此操作的任何人,我认为使用适当的 Web 调试代理(例如 Fiddler (.net) 或 Burp Suite (java))武装自己非常重要,以便您可以详细检查每个 REST 调用以了解特定的传递给 API 的数据的格式。
在我的具体情况下,我注意到curl在表单数据的每个部分上方插入了一个空行 - 因此为了扩展@jklemmack的示例,它将如下所示:
    $bodyLines = (
        "--$boundary",
        "Content-Disposition: form-data; name=`"formfield1`"",
        '',
        $formdata1,
        "--$boundary",
        "Content-Disposition: form-data; name=`"formfield2`"",
        '',
        $formdata2,
        "--$boundary",
        "Content-Disposition: form-data; name=`"formfield3`"; filename=`"$name_of_file_being_uploaded`"",
        "Content-Type: application/json",
        '',
        $content_of_file_being_uploaded,
        "--$boundary--"
    ) -join $LF
希望这可以在将来为某人节省很多时间!
我还仍然同意,如果您需要从头开始执行此操作,并且可以选择直接使用curl 本机二进制文件(同时确保围绕安全性和合规性进行尽职调查),那么您可以利用它的成熟度及其提供的便利性。使用卷曲。最好由整个curl 社区对这种多部分逻辑进行严格测试和维护,而不是由内部开发或运营团队承担责任。
使用 PowerShell Core,这应该可以使用新-Form参数开箱即用。
请参阅:https : //docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7
$Uri = 'https://api.contoso.com/v2/profile'
$Form = @{
    firstName  = 'John'
    lastName   = 'Doe'
    email      = 'john.doe@contoso.com'
    avatar     = Get-Item -Path 'c:\Pictures\jdoe.png'
    birthday   = '1980-10-15'
    hobbies    = 'Hiking','Fishing','Jogging'
}
$Result = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form
我需要传递标头和更多参数(insert=true和debug=true)以及文件内容。这是我的版本,它通过@jklemmack 扩展了脚本。
param([string]$path)
$Headers = @{Authorization = "Bearer ***************"}
$Uri = 'https://host:8443/api/upload'
$fileBytes = [System.IO.File]::ReadAllBytes($path);
$fileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";
$bodyLines = ( 
    "--$boundary",
    "Content-Disposition: form-data; name=`"insert`"$LF",
    "true$LF",
    "--$boundary",
    "Content-Disposition: form-data; name=`"debug`"$LF",
    "true$LF",    
    "--$boundary",
    "Content-Disposition: form-data; name=`"file`"; filename=`"$path`"",
    "Content-Type: application/octet-stream$LF",
    $fileEnc,
    "--$boundary--$LF" 
) -join $LF
Invoke-RestMethod -Uri $Uri -Headers $Headers -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines