- [ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4.
- Indicator value retreived by EA code lags 5 minutes behind what shows on the chart
- retrieving Open and Close values in MQL5
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);
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()) ); }
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.
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!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use