I was trying to send a discord webhook with C# like this:
public class DiscordWebhook(string content)
{
public string content { get; set; } = content;
}
...
var message = "```ansin";
foreach (var pl in top10)
{
var sb = new StringBuilder(Plugin.Config.WebhookMessage);
...
message += sb + "n";
}
message += "```";
PostSendMessage(JsonSerializer.ToJsonString(new DiscordWebhook(message)));
...
public async Task PostSendMessage(string messageContent)
{
using var client = new HttpClient();
var content = new StringContent(messageContent, Encoding.UTF8, "application/json");
try
{
var response =
await client.PostAsync(
$"https://discord.com/api/webhooks/{Plugin.Config.WebhookId}/{Plugin.Config.WebhookToken}",
content);
Log.Info(response.Content.ReadAsStringAsync().Result);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
Log.Error(e);
}
}
but the 400 Bad Request Error Happens:
{"message": "The request body contains invalid JSON.", "code": 50109}
Logged the JsonSerializer.ToJsonString(new DiscordWebhook(message))
, paste into nodejs and sent a request, and it was working. It works when I remove the code block (“`)