Help on how to use WebRequest correctly.

 

Hi all,

I have been trying to use Webrequest in getting community sentiment into my EA.

But I keep getting the above error 'WebRequest' - no one of the overloads can be applied to the function call

Any help to fix this will be greatly appreciated.


Below is the section of my code that needs to be corrected.

//+------------------------------------------------------------------+
//| Perform web request                                              |
//+------------------------------------------------------------------+
int PerformWebRequest(
      const string method, 
      const string url, 
      const string headers, 
      const string data, 
      int result_size, 
      char &result[], 
      string &result_headers
   )
{
    // Ensure the WebRequest function is allowed in the terminal settings
    if (!TerminalInfoInteger(TERMINAL_DLLS_ALLOWED) || !TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
    {
        Print("WebRequest is not allowed. Please enable it in the terminal settings.");
        return -1;
    }

    // Set the timeout for the WebRequest
    int timeout = 5000; // 5 seconds

    // Initialize result array and headers
    ArrayResize(result, 0); // Ensure the result array is empty initially
    result_headers = "";

    // Perform the WebRequest
    int res = WebRequest(
        method,           // HTTP method
        url,              // URL
        headers,          // Headers
        timeout,          // Timeout in milliseconds
        data,             // POST data
        result,           // Result array
        result_size,      // Result size
        result_headers    // Result headers
    );

    // Check for errors
    if (res == -1)
    {
        int errorCode = GetLastError();
        Print("WebRequest failed with error #", errorCode, ": ", ErrorDescription(errorCode));
        return errorCode;
    }

    // Log the response
    Print("WebRequest completed. HTTP response code: ", res);
    Print("Response headers: ", result_headers);
    Print("Response body: ", CharArrayToString(result, 0, result_size));

    // Return the HTTP response code
    return res;
}
//+------------------------------------------------------------------+
//| Helper function to convert error codes to descriptions           |
//+------------------------------------------------------------------+
string ErrorDescription(int errorCode)
{
    switch (errorCode)
    {
        case 1: return "No error";
        case 2: return "Common error";
        case 3: return "Invalid trade parameters";
        case 4: return "Server is busy";
        case 5: return "Old version of the client terminal";
        case 6: return "No connection with the trade server";
        case 7: return "Not enough rights";
        case 8: return "Too frequent requests";
        case 9: return "Malfunction (critical error)";
        // Add more error codes as needed
        default: return "Unknown error";
    }
}

//+------------------------------------------------------------------+
//| Helper function to convert a char array to a string              |
//+------------------------------------------------------------------+
string CharArrayToString(char &array[], int start, int length)
{
    string result = "";
    for (int i = start; i < start + length; i++)
    {
        result += CharToString(array[i]);
    }
    return result;
}
//+------------------------------------------------------------------+
//| Login to Myfxbook and retrieve the session ID                   |
//+------------------------------------------------------------------+
string LoginToMyfxbook()
{
    string url = "";
    string headers = "";
    string postData = "username=" + MyfxbookUsername + "&password=" + MyfxbookPassword;
    int result_size = 0;
    char result[];
    string result_headers;
    
    int res = PerformWebRequest(
        "POST",               // HTTP method
        url,                  // URL
        headers,              // Headers (empty)
        postData,             // Data (POST data)
        result_size,          // Result size
        result,               // Result
        result_headers        // Result headers
    );
    
    if (res == -1)
    {
        Print("WebRequest failed with error #", GetLastError());
        return "";
    }
    
    // Convert result to string
    string response = CharArrayToString(result, 0, result_size);
    Print("Login API Response: ", response);
    
    // Parse the response to get session ID
    return ParseSessionIdFromJSON(response);
}

//+------------------------------------------------------------------+
//| Get community sentiment from Myfxbook                           |
//+------------------------------------------------------------------+
double GetCommunitySentiment()
{
    string sessionId = LoginToMyfxbook();
    if (sessionId == "")
    {
        Print("Failed to obtain session ID.");
        return 0.0;
    }
    
    string url = "" + sessionId;
    string headers = "";
    int result_size = 0;
    char result[];
    string result_headers;
    
    int res = PerformWebRequest(
        "GET",                // HTTP method
        url,                  // URL with session ID
        headers,              // Headers (empty)
        "",                   // Data (not used for GET)
        result_size,          // Result size
        result,               // Result
        result_headers        // Result headers
    );
    
    if (res == -1)
    {
        Print("WebRequest failed with error #", GetLastError());
        return 0.0;
    }
    
    // Convert result to string
    string response = CharArrayToString(result, 0, result_size);
    Print("API Response: ", response);
    
    // Parse the response to get community sentiment
    double sentiment = ParseSentimentFromJSON(response);
    return sentiment;
}


Documentation on MQL5: Network Functions / WebRequest
Documentation on MQL5: Network Functions / WebRequest
  • www.mql5.com
The function sends an HTTP request to a specified server. The function has two versions: 1. Sending simple requests of type "key=value...