我想使用powershell invoke-restmethod使用x-www-form-urlencoded格式传递一些body参数。难道这在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"