MQL5: Calling a protected REST API to receive client data

 

Hello Folks,

I assume that its a quite easy question for you but I need your help.

I got a website where I want my future EA clients to but their AccountIDs for a license Check. 

When I call the API via Postman I get the data, however I am unable to adapt the Webrequest() function accordingly to get it to work, please help :)

The API will return a JSON, that I want to parse lateron to check for the AccountIDs and if the Account is still valid

From my understanding / the documentation I dont need any includes

void OnStart()
{
    string url = "https://URL/wp-json/mp/v1/members/10"; // URL would be my Domain name 
    string headers = "Authorization: Bearer XYZ";   //XYZ would be my API Key
    string result;

    int request = WebRequest("GET", url, headers, 5000, NULL, result, "");

    if(request == 200)
    {
        // Success
        Print("API call successful. Response: ", result);

        // Parse the response data here
    }
    else
    {
        // Error
        Print("API call failed. Error code: ", request);
    }
}

Thank you very much!

Documentation on MQL5: Network Functions / WebRequest
Documentation on MQL5: Network Functions / WebRequest
  • www.mql5.com
WebRequest - Network Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
johkoch: I assume that its a quite easy question for you but I need your help. I got a website where I want my future EA clients to but their AccountIDs for a license Check. When I call the API via Postman I get the data, however I am unable to adapt the Webrequest() function accordingly to get it to work, please help :) The API will return a JSON, that I want to parse lateron to check for the AccountIDs and if the Account is still valid. From my understanding / the documentation I dont need any includes

You have not explained the nature of the problem or issue you are having. You simply stated that you are "unable" but not what it is you are unable to do specifically.

Also, please remember that you have to give permission for the URL to be allowed access, in the Options:


 
Fernando Carreiro #:

You have not explained the nature of the problem or issue you are having. You simply stated that you are "unable" but not what it is you are unable to do specifically.

Also, please remember that you have to give permission for the URL to be allowed access, in the Options:


You are right, sorry for that. 🙏😁

When compiling the code i get an error it says:
'WebRequest' - no one of the overloads can be applied to the function call 
could be one of 2 function(s) 
   built-in: int WebRequest(const string,const string,const string,const string,int,const char&[],int,char&[],string&) 
   built-in: int WebRequest(const string,const string,const string,int,const char&[],char&[],string&)
 
johkoch #: You are right, sorry for that. 🙏😁 When compiling the code i get an error it says: 'WebRequest' - no one of the overloads can be applied to the function call 

Untested code to serve only as an example ...

void OnStart() {
   string   url = "https://URL/wp-json/mp/v1/members/10", // URL would be my Domain name 
            headers = "Authorization: Bearer XYZ",   //XYZ would be my API Key
            result_headers;
   char     data[] = {},
            result[];
   int request = WebRequest( "GET", url, headers, 5000, data, result, result_headers );
   if( request == 200 ) {
      // Success
         string result_string = CharArrayToString( result, 0, WHOLE_ARRAY, CP_UTF8 );
         Print( "API call successful. Response: ", result_string );
      // Parse the response data here
   } else {
      // Error
         Print( "API call failed. Error code: ", request );
   };
};


 
Fernando Carreiro #:

Untested code to serve only as an example ...


Thank you very much, I will give it a try tomorrow :)
 
johkoch #:
Thank you very much, I will give it a try tomorrow :)

Hey.

Iam doing the same thing, I just tried your code but it didnt work. I was wondering if you got the solution