discord webhook can not send empty message
Asked Answered
S

2

6

I have written this small PoC for discord webhooks and i am getting error that Can not send empty string. I tried to google but couldn't find a documentation or an answer

here is my code

import requests

discord_webhook_url = 'https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxx/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

data = {'status': 'success'}
headers = {'Content-Type': 'application/json'}

res = requests.post(discord_webhook_url, data=data, headers=headers)

print(res.content)
Sweepstakes answered 29/9, 2018 at 4:30 Comment(0)
N
8

I'm late, but I came across this issue recently, and seeing as it has not been answered yet, I thought I document my solution to the problem.

For the most part, it is largely due to the structure of the payload being wrong.

https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html provides an example of a working structure. https://discordapp.com/developers/docs/resources/channel#create-message is the official documentation.

I was also able to get a minimum test case working using: {"content": "Test"}.

If it still fails after that with the same error, the likely causes are:

  • If using curl, check to make sure there are no accidental escape / backslashes \
  • If using embeds with fields, ensure there are no empty values

When in doubt, ensure all values are populated, and not "". Through trial-and-error / the process of cancellation, you can figure out exactly what key-value pair is causing an issue, so I suggest playing with the webhook via curl before turning it into a full program.

Nally answered 2/1, 2019 at 16:45 Comment(2)
how would i populate ALL values when i don't even want them to be displayed? Shouldn't just leaving them as null a better idea?Sweepstakes
I haven't tried setting value: null, but yes, you can just not include the field. There is no requirement for you to have them in the first place.Nally
P
0

In case anyone else finds this like I did, Ian's answer got me most of the way there but it's not 100%. At the moment, the developer docs state: Note that when sending a message, you must provide a value for at least one of content, embeds, components, or file.

So, for the above, the payload would have to be something like:

data = {'content': 'status = success'}

https://discord.com/developers/docs/resources/webhook#execute-webhook

Padishah answered 24/6, 2023 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.