PowerShell - Slack API - 房间历史记录 - Post 带有 x-www-form-urlencoded 参数的方法

PowerShell - Slack API - Room History - Post method with x-www-form-urlencoded parameters

我想使用 x-www-form-urlencoded 格式使用 powershell invoke-restmethod 传递一些正文参数。不要认为这在 PostMan 中工作正常。我的代码在下面但不起作用。我如何在 powershell 中完成此操作?

$param = [System.Web.HttpUtility]::UrlEncode("channel:channelID
Content-Type:application/x-www-form-urlencoded
limit:50")


$uri="https://MySlackWebsite.com/api/channels.history"

$test2 = Invoke-RestMethod -Method POST -Uri $uri -Headers $headerJson -Body $param

这是一个示例脚本,说明如何使用 POST 请求从频道检索消息列表。

$postParams = @{token='xoxp-XXX';channel='C12345678'}
$test2 = Invoke-WebRequest -Uri https://slack.com/api/channels.history -Method POST -Body $postParams
Write-Host $test2

它测试并基于 this answer 关于如何使用 PowerShell 创建 POST 请求。

我使用从@Erik Kalkoken 那里得到的指导使它与以下内容一起工作。

$headerJson = @{Authorization="Bearer xoxp-xyz"}
$postParams = @{channel='roomID';limit=50}
$uri="https://slackserver.com/api/channels.history"

Invoke-RestMethod -Method POST -Uri $uri -Headers $headerJson -Body $postParams -ContentType "application/x-www-form-urlencoded"