MT5 webrequest not responding

 
//  MT5 – minimal heartbeat tester
#property strict

input string CONFIG_FILE_NAME = "***\\***_mt5_config.ini";

string gApiKey       = "";
string gAccountId    = "";
string gIngestUrl    = "";
string gHeartbeatUrl = "";

//----------------- helpers -----------------
string TrimBoth(const string s)
{
   int st = 0;
   int en = StringLen(s) - 1;
   while(st <= en && StringGetCharacter(s, st) <= 32) st++;
   while(en >= st && StringGetCharacter(s, en) <= 32) en--;
   if(st > en) return "";
   return StringSubstr(s, st, en - st + 1);
}

string StripBOM(const string s)
{
   if(StringLen(s) >= 1 && StringGetCharacter(s, 0) == 0xFEFF)
      return StringSubstr(s, 1);
   return s;
}

bool LoadConfig()
{
   int h = FileOpen(CONFIG_FILE_NAME, FILE_READ | FILE_TXT | FILE_ANSI);
   if(h == INVALID_HANDLE)
   {
      Print(": Config NOT found: ", CONFIG_FILE_NAME);
      return false;
   }

   while(!FileIsEnding(h))
   {
      string raw = FileReadString(h);
      if(StringLen(raw) == 0) continue;

      raw = StripBOM(raw);
      string line = TrimBoth(raw);
      if(StringLen(line) == 0) continue;

      string first = StringSubstr(line, 0, 1);
      if(first == "#" || first == "[" || first == ";") continue;

      int eq = StringFind(line, "=");
      if(eq <= 0) continue;

      string key = TrimBoth(StringSubstr(line, 0, eq));
      string val = TrimBoth(StringSubstr(line, eq + 1));

      if(key == "ApiKey")         gApiKey       = val;
      else if(key == "AccountId") gAccountId    = val;
      else if(key == "IngestUrl") gIngestUrl    = val;
      else if(key == "HeartbeatUrl") gHeartbeatUrl = val;
   }
   FileClose(h);

   if(StringLen(gApiKey) == 0 || StringLen(gAccountId) == 0 || StringLen(gIngestUrl) == 0)
   {
      Print(": Config missing ApiKey/AccountId/IngestUrl");
      return false;
   }

   if(StringLen(gHeartbeatUrl) == 0)
   {
      int pos = StringFind(gIngestUrl, "/ingest");
      if(pos >= 0)
         gHeartbeatUrl = StringSubstr(gIngestUrl, 0, pos) + "/heartbeat";
      else
         gHeartbeatUrl = gIngestUrl + "/heartbeat";
   }

   Print(": Loaded config. Ingest=", gIngestUrl, " Heartbeat=", gHeartbeatUrl);
   return true;
}

//----------------- networking -----------------
int SendHeartbeat()
{
   if(StringLen(gHeartbeatUrl) == 0 || StringLen(gApiKey) == 0)
   {
      Print(": Heartbeat skipped, missing url/key");
      return -1;
   }

   uchar data[];
   StringToCharArray("{}", data, 0, WHOLE_ARRAY, CP_UTF8);

   uchar resultData[];
   string resultHeaders;
   string headers = "Content-Type: application/json\r\nX-API-Key: " + gApiKey + "\r\n";

   ResetLastError();
   Print(": Calling WebRequest to ", gHeartbeatUrl);

   int res = WebRequest("POST", gHeartbeatUrl, headers, 60, data, resultData, resultHeaders);
   int err = GetLastError();
   string body = CharArrayToString(resultData);

   PrintFormat(": Heartbeat debug -> res=%d, lastError=%d, body='%s'",
               res, err, body);

   return res;
}


//----------------- EA lifecycle -----------------
int OnInit()
{
   if(!LoadConfig())
      return INIT_FAILED;

   // DIRECT heartbeat test on init
   Print(": OnInit -> SendHeartbeat()");
   SendHeartbeat();
   Print(": OnInit finished.");

   return INIT_SUCCEEDED;
}

void OnDeinit(const int reason) {}
void OnTick() {}

Hello,


Im trying to create a link between my node, express server and my MT5 terminal however i have run into an issue i cant solve. whenever i send a request from the terminal it just "hangs"/dosent respond untill it times out and responds with  a 1003, 5203 error. I have the server hosted on Railway with a custom domain, hitting the domain from curl works without issue. the urls are all allowed. il attach the code bellow: 

 
i will also add that it dosent want to comunicate with the server when im on localhost/127.0.0.1
 
Elias Jovancic:

Hello,


Im trying to create a link between my node, express server and my MT5 terminal however i have run into an issue i cant solve. whenever i send a request from the terminal it just "hangs"/dosent respond untill it times out and responds with  a 1003, 5203 error. I have the server hosted on Railway with a custom domain, hitting the domain from curl works without issue. the urls are all allowed. il attach the code bellow: 

this looks like an MT5 WebRequest issue rather than your Node server. In MQL5 the timeout param is milliseconds, so your 60 is only 60ms (not 60s) — try 10000–15000 . Also MT5 often hits an internal read timeout (~10s) and returns 1003/5203, so make sure /heartbeat responds super fast (no heavy work/DB calls). If you’re testing in Strategy Tester, WebRequest won’t work there — run it on a live chart. Since you’re on Railway, check if Serverless/App sleeping is enabled (cold starts can be slow) — disabling it usually fixes first-request hangs.