I want to send message from mt4 to discord but it has been unable to succeed.

 

The error message is that The request body contains invalid JSON.", "code":50109. I don't know how to correct the problem. Can anyone help me? Thanks!

bool SendDiscordMessage(string webhookUrl, string message)

{

    string headers = "Content-Type: application/json\r\n";
    string jsonPayload = StringFormat("{\"content\": \"%s\",}", message);
    
    //Print out the actual JSON sent and check the format
    Print("Sending JSON: ", jsonPayload);

    char postData[];
    StringToCharArray(jsonPayload, postData);

    char resultData[];
    string resultHeaders;
    int timeout = 5000;  // 5 seconds timeout

    ResetLastError();

    // Send WebRequest
    int response = WebRequest("POST", webhookUrl, headers, timeout, postData, resultData, resultHeaders);

    // Check response
    if (response == -1) {
        int errorCode = GetLastError();
        PrintFormat("Error in WebRequest. Error code: %d", errorCode);
        return false;
    } else if (response == 204) {
        Print("Message successfully sent to Discord!");
        return true;
    } else {
        string result = CharArrayToString(resultData);
        PrintFormat("Sending failed! HTTP status code: %d, response content: %s", response, result);
        return false;
    }
}

// Test example
void OnStart() {
    string webhookUrl = "https://discordapp.com/api/webhooks/1317328743244496967/xxxxxx";
    string message = "Hello,Discord";

    bool result = SendDiscordMessage(webhookUrl, message);
    if (result) {
        Print("Message has been sent!");
    } else {
        Print("Message sending failed!");
    }
}
OOP in MQL5 by Example: Processing Warning and Error Codes
OOP in MQL5 by Example: Processing Warning and Error Codes
  • www.mql5.com
The article describes an example of creating a class for working with the trade server return codes and all the errors that occur during the MQL-program run. Read the article, and you will learn how to work with classes and objects in MQL5. At the same time, this is a convenient tool for handling errors; and you can further change this tool according to your specific needs.
 

please edit your post -

------------------

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Place the cursor on WebRequest and press F1. Then use the example and replace it step by step with your code.
 
Sergey Golubev #:

please edit your post -

------------------

When you post code please use the CODE button (Alt-S)!

thank you very much.
 
Carl Schreiber #:
Place the cursor on WebRequest and press F1. Then use the example and replace it step by step with your code.
Thank you, I have tried it, but still can't find the problem.
 
Try to remove that comma in your json request.
Change
\"%s\",}
\"%s\"}
 
Soewono Effendi #:
Try to remove that comma in your json request.
Change
Thank you, I tried it but still the same problem.
 
The problem you are encountering happens when your string is converted into a char array.
In MQL5, strings are automatically terminated with the character '\0' (null character).
If you now convert the entire string into a char array, the last character of your char array is not "}" but '\0' . This results in an invalid JSON when sent to Discord.

This can be verified with the following debug output in your example script:
// For debugging: print the JSON payload char by char
for(int i = 0; i < ArraySize(postData); i++){
   Print("postData[", i, "] = ", postData[i], " (", CharToString(postData[i]), ")");
}
** * ** ** **
KK 0 12:17:17.955 Test (BTCUSD,M1) postData[23] = 111 (o)
DS 0 12:17:17.955 Test (BTCUSD,M1) postData[24] = 114 (r)
LJ 0 12:17:17.955 Test (BTCUSD,M1) postData[25] = 100 (d)
GJ 0 12:17:17.955 Test (BTCUSD,M1) postData[26] = 34 (")
RM 0 12:17:17.955 Test (BTCUSD,M1) postData[27] = 125 (})

DJ 0 12:17:17.955 Test (BTCUSD,M1) postData[28] = 0 ()


So, you can either limit the length of the string:

StringToCharArray(jsonPayload, postData, 0, StringLen(jsonPayload));
Alternatively, you can check whether the last character of the array is '\0' and remove it if necessary:
// If last char is \0, remove it
if(postData[ArraySize(postData) - 1] == 0){
    ArrayResize(postData, ArraySize(postData) - 1);
}
I tested your script with both approaches, and both methods work as expected.
I hope I could help and wish you happy holidays! 😊
 
Lukas Roth #:
The problem you are encountering happens when your string is converted into a char array.
In MQL5, strings are automatically terminated with the character '\0' (null character).
If you now convert the entire string into a char array, the last character of your char array is not "}" but '\0' . This results in an invalid JSON when sent to Discord.

This can be verified with the following debug output in your example script:
** * ** ** **
KK 0 12:17:17.955 Test (BTCUSD,M1) postData[23] = 111 (o)
DS 0 12:17:17.955 Test (BTCUSD,M1) postData[24] = 114 (r)
LJ 0 12:17:17.955 Test (BTCUSD,M1) postData[25] = 100 (d)
GJ 0 12:17:17.955 Test (BTCUSD,M1) postData[26] = 34 (")
RM 0 12:17:17.955 Test (BTCUSD,M1) postData[27] = 125 (})

DJ 0 12:17:17.955 Test (BTCUSD,M1) postData[28] = 0 ()


So, you can either limit the length of the string:

Alternatively, you can check whether the last character of the array is '\0' and remove it if necessary:
I tested your script with both approaches, and both methods work as expected.
I hope I could help and wish you happy holidays! 😊
Thank you so much! It is executable. I successfully sent the message in English. Bless you!  😊