加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何使用PowerShell Invoke-RestMethod发送multipart / form-dat

发布时间:2020-12-13 18:11:47 所属栏目:Linux 来源:网络整理
导读:我正在尝试通过Invoke-RestMethod发送一个文件,类似于curl与-F开关的上下文. 卷曲示例 curl -F FileName=@"/path-to-file.name" "https://uri-to-post" 在powerhell中,我尝试过这样的事情: $uri = "https://uri-to-post"$contentType = "multipart/form-dat
我正在尝试通过Invoke-RestMethod发送一个文件,类似于curl与-F开关的上下文.

卷曲示例

curl -F FileName=@"/path-to-file.name" "https://uri-to-post"

在powerhell中,我尝试过这样的事情:

$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响应,显示没有有效载荷被发送.

我也试图使用-InFile参数,没有运气.

我看过许多使用.net类的例子,但试图用新的Powershell 3命令来保持这个简单.

有没有人有任何指导或经验使这项工作?

解决方法

这里的问题是API需要一些其他参数.初始请求需要一些参数来接受原始内容并指定filename / size.设置完毕并获取正确的链接后,我可以使用:
Invoke-RestMethod -Uri $uri -Method Post -InFile $filePath -ContentType "multipart/form-data"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读