Help send request function MQL4

 

Hello, 

I have created API with port 80 in local server. I want to send data from terminal to API but it shows error 4029. I tried many ways but it didnt work but it worked well in mt5.

Below is my short function. Please help me to fix it or guide me to fix it. Thank you guys so much 

#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//+------------------------------------------------------------------+
//| Save swap point function                                         |
//+------------------------------------------------------------------+

void SendPrice(long account, string sym, double bid, double ask, string date)
{
   int    res;       // To receive the operation execution result
   char   data[];    // Data array to send POST requests
   string jsonData;  // JSON body for price data

   // Create JSON body for price data
   jsonData = "{\"account\": \"" + account + "\","; 
   jsonData += "\"symbol\":\"" + sym + "\","; 
   jsonData += "\"bid\":" + DoubleToString(bid, 5) + ","; 
   jsonData += "\"ask\":" + DoubleToString(ask, 5) + ","; 
   jsonData += "\"date\":\"" + date + "\"}"; 

   // Convert JSON data to char array
   ArrayResize(data, StringToCharArray(jsonData, data, 0, WHOLE_ARRAY, CP_UTF8) - 1);

   // Set headers for the POST request
   string headers = "Content-Type: application/json\r\n";

   // Send the price data as a JSON POST request
   res = WebRequest("POST", "http://127.0.0.1:80/api/prices", headers, 0, data, data, jsonData);
   
   // Check if sending price data was successful
   if(res != 200)
   {
      Print("Error sending price data to server #"+(string)res+", LastError="+(string)GetLastError());
   }

   Print("Price data sent successfully: ", jsonData);
}


void GetPrice()
{
   int totalSymbol = SymbolsTotal(true);
   for (int i = 0; i < totalSymbol; i++)
   {
      int account_number = AccountNumber();
      string symbol = SymbolName(i, true);
      double bid = MarketInfo(symbol, MODE_BID);
      double ask = MarketInfo(symbol, MODE_ASK);
      string date = TimeToString(TimeCurrent(), TIME_DATE | TIME_MINUTES);
      SendPrice(account_number, symbol, bid, ask, date);
   }
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Call GetPrice when initialization
   //GetPrice();
   return (INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Deinitialization logic (if needed)
   EventKillTimer();
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // Call GetPrice every tick
   GetPrice();
}

void OnStart()
  {
//---
   GetPrice();
  }
//+------------------------------------------------------------------+
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • 2024.12.11
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 

Good-looking code!

NOTE: Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2024.12.12
  • www.mql5.com
The largest freelance service with MQL5 application developers
 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893