首页 > 解决方案 > PowerShell Discord Webhook

问题描述

使用此代码时出现错误

$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

这是因为什么?

标签: powershelldiscord

解决方案


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

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

请参阅:https ://github.com/discord/discord-api-docs/issues/1210


推荐阅读