PowerShell Discord 网络钩子

时间:2021-03-24 08:27:21

标签: powershell discord

我在使用此代码时遇到错误

$uri = "https://discord.com/api/webhooks/760116538895499265/yhQdNc10asfDzt8hldAJp32et1Gp_1-mvqIGPWZiAMcOwFjb5aqf7uNEF7MX6i15UKJK"

$hash = @{ "content" = "heyyy"; }

$JSON = $hash | convertto-json

Invoke-WebRequest -uri $uri -Method POST -Body $JSON
Invoke-WebRequest : {"message": "Cannot send an empty message", "code": 50006}
At line:7 char:1
+ Invoke-WebRequest -uri $uri -Method POST -Body $JSON
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

这是什么原因?

1 个答案:

答案 0 :(得分:3)

您应该在请求中为 application/json 指定 Content-Type:

 Invoke-WebRequest -uri $uri -Method POST -Body $JSON -Headers @{'Content-Type' = 'application/json'}

见:https://github.com/discord/discord-api-docs/issues/1210