Discord webhooks with jdk21

  Kiến thức lập trình

So I recently changed the java version from jdk17 to jdk21, but since then webhooks haven’t been sending to the discord, this is the error message that was sent

Failed to send webhook. Response code: 400
Response: {“message”: “The request body contains invalid JSON.”, “code”: 50109}

Message: {“tts”:false,”embeds”:[{“color”:0,”title”:”Rare drop received!”,”type”:”rich”,”fields”:[]}],”content”:”Rare drop received: message!”}
Sent: {“tts”:false,”embeds”:[{“color”:0,”title”:”Rare drop received!”,”type”:”rich”,”fields”:[]}],”content”:”Rare drop received: message!”}
Got: {“message”: “The request body contains invalid JSON.”, “code”: 50109}

There doesn’t seem anything wrong with the format of the json, unsure if anything got changed with jdk21 that would make it so they stopped working as it’s strange

Message message = new Message();

            Embed embedMessage = new Embed();
            String mode = "";
            switch (player.getGameMode()) {
                case IRONMAN:
                    mode = "[Ironman] ";
                    break;
                case HARDCORE_GROUP_IRONMAN:
                    mode = "[Hardcore Group Ironman] ";
                    break;
                case GROUP_IRONMAN:
                    mode = "[Group Ironman] ";
                    break;
                case HARDCORE_IRONMAN:
                    mode = "[Hardcore Ironman] ";
                    break;
                case ULTIMATE_IRONMAN:
                    mode = "[Ultimate Ironman] ";
                    break;
            }
            System.out.println("sendDiscordMessage3");
            embedMessage.setTitle(mode + "Rare drop received!");
            String npcdesc = npcDescriptiveName == null ? "" : " from " + npcDescriptiveName;
            embedMessage.setDescription(discordMessage + npcdesc + "!");
             embedMessage.setColor(8917522);

            Thumbnail thumbnail = new Thumbnail();
            thumbnail.setUrl("https://static.runelite.net/cache/item/icon/" + itemId + ".png");
            embedMessage.setThumbnail(thumbnail);

            Footer footer = new Footer();
            if(killcount == -1)
                footer.setText("");
            else
                footer.setText(player.getDifficulty().Name + " mode" + " - KC: " + killcount + ".");
            embedMessage.setFooter(footer);



     
            System.out.println("Sending Discord message: " + message.toJson());
            message.setEmbeds(embedMessage);
            message.setContent("Rare drop received: " + discordMessage + "!");
            webhook.sendMessage(message.toJson());

This is where the information being sent is build

and this is the method that sends the json to the discord

 public void sendMessage(JSONObject message) {
        try {
            // Convert the JSON message to a string
            String messageString = message.toString();

            // Create the HttpRequestWithBody object
            HttpRequestWithBody request = Unirest.post(url)
                    .header("Content-Type", "application/json");

            // Set the request body
            request.body(messageString);

            // Make the HTTP request
            HttpResponse<String> res = request.asString();

            if (res.getStatus() == 200) {
                System.out.println("Webhook sent successfully.");
            } else {
                System.out.println("Failed to send webhook. Response code: " + res.getStatus());
                System.out.println("Response: " + res.getBody());
                System.out.println("Message: " + messageString);
            }

            if (verbose) {
                System.out.println("Sent: " + messageString);
                System.out.println("Got: " + res.getBody());
            }
        } catch (UnirestException e) {
            ServerWrapper.logError("Failed to send webhook: " + message.toString(), e);
        }
    }

I noticed just sending text to the discord using webhooks is fine it’s just when you try to build a json to make it look prettier it seems to have issues

I have tried sending regular text and that sends fine so i know it’s nothing with the url, the json that build i’ve ran through json checkers and it’s fine structurally so not sure what it could be

New contributor

Reprisal Gaming is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT