如何使用PowerShell Invoke-RestMethod发送multipart / form-data [英] How to send multipart/form-data with PowerShell Invoke-RestMethod
问题描述
我试图通过Invoke-RestMethod在类似上下文中使用-F开关发送文件。
I'm trying to send a file via Invoke-RestMethod in a similar context as curl with the -F switch.
卷曲示例
curl -F FileName=@"/path-to-file.name" "https://uri-to-post"
<我尝试过这样的:
In powershell, I've tried something like this:
$uri = "https://uri-to-post"
$contentType = "multipart/form-data"
$body = @{
"FileName" = Get-Content($filePath) -Raw
}
Invoke-WebRequest -Uri $uri -Method Post -ContentType $contentType -Body $body
}
如果我检查提琴手,我看到的身体包含原始二进制数据,但我得到一个200响应回显示没有有效载荷已发送。
If I check fiddler I see that the body contains the raw binary data, but I get a 200 response back showing no payload has been sent.
我也尝试使用-InFile参数没有运气。
I've also tried to use the -InFile parameter with no luck.
有没有人有任何指导或经验使这项工作?
Does anyone have any guidance or experience making this work?
推荐答案
这里的问题是API需要一些额外的参数。初始请求需要一些参数来接受原始内容并指定文件名/大小。设置完成并返回正确的链接提交后,我可以使用:
The problem here was what the API required some additional parameters. Initial request required some parameters to accept raw content and specify filename/size. After setting that and getting back proper link to submit, I was able to use:
Invoke-RestMethod -Uri $uri -Method Post -InFile $filePath -ContentType "multipart/form-data"
这篇关于如何使用PowerShell Invoke-RestMethod发送multipart / form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!