MQL5 WebRequest send json
MQL5 WebRequest send json
Loading saved threads...
yohans shegaw · External communityPost link
External question — Stack Overflow Stack Exchange
Author: yohans shegaw
Original post: https://stackoverflow.com/questions/79019248
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
input string WebhookURL = "https://discord.com/api/webhooks/1288029176930242621/t-npSVLA5x8hv2QqAI8KfXbsiAXAtBMQcYyZyuuSYeFBOzr-AT-ldGGPAIXwUgeMOfB0";
void SendDiscordMessage(string message)
{
Print("Raw message: ", message);
// Escape newlines in the message
StringReplace(message, "\n", "\\n");
string json = "{\"content\":\"" + message + "\"}";
Print("Final JSON Payload: ", json);
char data[];
StringToCharArray(json, data);
ArrayResize(data, StringLen(json) + 1); // Ensure the array has enough space
char result[];
string headers = "Content-Type: application/json";
Print("Sending with headers: ", headers);
string result_headers;
int res = WebRequest("POST", WebhookURL, headers, 5000, data, result, result_headers);
if (res < 0)
{
Print("Error sending message: ", GetLastError());
return;
}
string response = CharArrayToString(result);
Print("HTTP Response Code: ", res);
Print("Response Raw Output: ", response);
}
void OnStart()
{
Print("Discord notification script running on MT5...");
int totalPositions = PositionsTotal();
Print("Total open positions: ", totalPositions);
for (int i = 0; i < totalPositions; i++)
{
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket))
{
string symbol = PositionGetString(POSITION_SYMBOL);
double entryPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double stopLoss = PositionGetDouble(POSITION_SL);
double takeProfit = PositionGetDouble(POSITION_TP);
// Format the message
string message = StringFormat("New Trade: %s\\nEntry: %.2f\\nSL: %.2f\\nTP: %.2f",
symbol, entryPrice, stopLoss, takeProfit);
Print("Complete message: ", message); // Log the complete message
SendDiscordMessage(message);
}
else
{
Print("Failed to select position with ticket: ", ticket);
}
}
}
Output is:
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Discord notification script running on MT5...
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Total open positions: 1
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Complete message: New Trade: MYMZ24\nEntry: 42574.00\nSL: 0.00\nTP: 0.00
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Raw message: New Trade: MYMZ24\nEntry: 42574.00\nSL: 0.00\nTP: 0.00
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Final JSON Payload: {"content":"New Trade: MYMZ24\nEntry: 42574.00\nSL: 0.00\nTP: 0.00"}
2024.09.24 18:03:05.733 TelegeramALert (MESZ24,H1) Sending with headers: Content-Type: application/json
2024.09.24 18:03:06.211 TelegeramALert (MESZ24,H1) HTTP Response Code: 400
2024.09.24 18:03:06.211 TelegeramALert (MESZ24,H1) Response Raw Output: {"message": "The request body contains invalid JSON.", "code": 50109}
Quote
Report
Elin Sancho · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Elin Sancho
Original post: https://stackoverflow.com/a/79070601
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
When you execute "StringToCharArray" for some reason is adding an extra character to the array which is recognized as an invalid character. So you just need to replace it. In my case I am replacing it with a blank space character and it is working.
StringToCharArray(json, data);
data[ArraySize(data) - 1] = ' ';
Quote
Report
LukiRoth · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: LukiRoth
Original post: https://stackoverflow.com/a/79325113
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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.
So, you can either limit the length of the string:
StringToCharArray(json, data, 0, StringLen(json));
Or 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(data[ArraySize(data) - 1] == 0){
ArrayResize(data, ArraySize(data) - 1);
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Elin Sancho Source score (net votes, not local likes): 1 Original post: https://stackoverflow.com/a/79070601 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. When you execute "StringToCharArray" for some reason is adding an extra character to the array which is recognized as an invalid character. So you just need to replace it. In my case I am replacing it with a blank space character and it is working. StringToCharArray(json, data); data[ArraySize(data) - 1] = ' ';
Checking account access…