Using Array Values outside of a WebRequest Function

 
Hello; I'm new to MQL4 and am attempting to retrieve values from a web request function to be used in a separate trading logic function but I cannot seem to get the data out of the local GetRequest function. There are 12 data points downloaded and I am using two of them, labeled p06 and p11, which are percent change values. These get refreshed with new values every 30 seconds. The data is being successfully imported via the WebRequest; I'm just not clear on the best approach for getting the values out of the function to be used elsewhere in the code. Any suggestions? Thank you!
int OnInit()
  {
//--- create timer
   EventSetTimer(30); //30 secs
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
//void OnTick()
//  {
//---
   
//  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
   GetRequest(); 
  }
//+------------------------------------------------------------------+
//| Web Request function                                             |
//+------------------------------------------------------------------+

void GetRequest() 
         string values[12];
         string cookie = NULL;
    
         //string referer = NULL;
    
         int timeout = 15000; // 15 secs
    
         char data[], result[];
    
         int data_size;
    
         string headers;
    
         string url = "http://localhost/analytics";
    
         data_size = ArraySize(data);
    
         int res; 
    
         res = WebRequest("GET", url, cookie, NULL, timeout, data, data_size, result, headers);
    
         if (res == -1) {
         Print("Error in WebRequest. Error code = ", GetLastError());
        
         } else {
            ConvertResponse(CharArrayToString(result), values);
          
            Print("p06 =>", values[5]);
            Print("p11 =>", values[10]);
           
            
            double p06 = values[5];
            double p11 = values[10];        
           
}


void ConvertResponse(string response, string& values[]) {
// parsing json object with this function...

         Print("Response =>", response);
   
         string value;
         string items[];
         ushort comma, dot;
   
         response = StringSubstr(response, 1, StringLen(response) - 2);
   
         comma = StringGetCharacter(",", 0);
         dot = StringGetCharacter(":", 0);
   
         int n = StringSplit(response, comma, items);
   
         for(int i = 0; i < n; i++) {
            string result[];
      
            StringSplit(items[i], dot, result);
      
            string key = result[0];
      
            if(key == "\"p06\"") {
               value = StringSubstr(result[1], 1, StringLen(result[1]) - 2);
            } else {
               value = result[1];
            }
      
            values[i] = value;
            
         }
         
}

 
double GetRequest(int index // the index of the array which should be returned
                           ){
         string values[12];
         string cookie = NULL;
    
         //string referer = NULL;
    
         int timeout = 15000; // 15 secs
    
         char data[], result[];
    
         int data_size;
    
         string headers;
    
         string url = "http://localhost/analytics";
    
         data_size = ArraySize(data);
    
         int res; 
    
         res = WebRequest("GET", url, cookie, NULL, timeout, data, data_size, result, headers);
    
         if (res == -1) {
         Print("Error in WebRequest. Error code = ", GetLastError());

         return -1;

         } else {
            ConvertResponse(CharArrayToString(result), values);
          
            Print("p06 =>", values[5]);
            Print("p11 =>", values[10]);
           
            
            return (double)values[index];      
           }
    return -1;
}

 double p06 = GetRequest(5);
 double p11 = GetRequest(10);
 
Pavel Shutovskiy:

Thank you!!! The code is now working successfully.
 
falconer:
Thank you!!! The code is now working successfully.

Don't actually use that code. I'm not sure if that was intended to troll you or not... but you do realize it's making two consecutive web request for the same data, right? Also, you shouldn't parse JSON like that because your code will fall apart the second a single char is out of place. Something like this is how you need to implement your code. 

#include <jason.mqh> // https://www.mql5.com/en/code/13663


bool getRequest(string &json_response)
{
   char req_body[];
   char response[];
   string response_headers;
   int status_code =  WebRequest( 
      "GET",           // HTTP method 
      "http://localhost/analytics",              // URL 
      "Content-Type: application/json",          // headers  
      15000,          // timeout 
      req_body,          // the array of the HTTP message body 
      response,        // an array containing server response data 
      response_headers   // headers of server response 
   );
   json_response = CharArrayToString(response);
   return (status_code == 200);
}

void OnStart()
{
   //string test;
   //if (!getRequest(test))
   //   return;
   string test = "{\"data\":[1,2,3,4,5], \"error\": false}";
   CJAVal json_parser;
   json_parser.Deserialize(test);
   printf("data[1]=%d, data[3]=%d, error=%s",
      json_parser["data"][1].ToInt(),
      json_parser["data"][3].ToInt(),
      string(json_parser["error"].ToBool())
   );
}
 
nicholi shen:

Don't actually use that code. I'm not sure if that was intended to troll you or not... but you do realize it's making two consecutive web request for the same data, right? Also, you shouldn't parse JSON like that because your code will fall apart the second a single char is out of place. Something like this is how you need to implement your code. 

Thank you, I will test this today.
 
nicholish en #:

Don't actually use that code. I'm not sure if that was intended to troll you or not... but you do realize it's making two consecutive web request for the same data, right? Also, you shouldn't parse JSON like that because your code will fall apart the second a single char is out of place. Something like this is how you need to implement your code. 

Thanks man for your reply, I have been struggling to get the web request and json parser work correctly. Turns out that the example that i was following was wrong which in results showing the request headers in my results array and i was not able to parse the data or even get the response code correct from my web server.

The only thing that i would like to add here (following your example): only use header "Content-Type" if needed you can send the request without it as well and it work the same, only send this header when you have complex data types like json, form data or anything else.

Rest again many thanks. Peace!