请求正文包含无效的JSON



我正试图使用Python请求POST到一个不协调的webhook URL,但无论何时出现embeds字段,它都会返回{'code': 50109, 'message': 'The request body contains invalid JSON.'}。如果我删除embeds并只保留content,它将发送而不会出现任何错误。

我的代码是:

url = "https://discord.com/api/webhooks/[redacted]/[redacted]"
headers = {
"Content-Type": "application/json"
}
data = {
"username": "Webhook",
"content": "Hello, World!",
"embeds": [{
"title": "Hello, Embed!",
"description": "This is an embedded message."
}]
}
res = requests.post(url, headers=headers, data=data)

我试过各种版本的Discord API,但结果总是一样的。

我通过更换使其正常工作

requests.post(url, headers=headers, data=data)

带有

requests.post(url, json=data)

试试这个。我认为请求库可能添加了一个名为content-type的头,该头与您的头Content-Type冲突,这使得Discord API返回错误:

url = "https://discord.com/api/webhooks/[redacted]/[redacted]"
headers = {
"content-type": "application/json"
}
data = {
"username": "Webhook",
"content": "Hello, World!",
"embeds": [{
"title": "Hello, Embed!",
"description": "This is an embedded message."
}]
}
res = requests.post(url, headers=headers, json=data)

最新更新