Hi everyone,
I’m learning how MQL5 handles external web requests.
I want to understand how we can connect and fetch simple JSON data from an external API just for testing purposes (for example, weather data or demo information).
Could anyone please tell me the best method to call such data securely inside an Expert Advisor or script?
Should we use Web Requests or another approach for handling APIs in MQL5?
I really appreciate any help you can provide.
- How to integrate a custom MQL5 indicator with Python to receive real-time data?
- Call data in CSV format
- Trouble with OpenAI API call in MQL5 You didn t provide an API key error
Please search 🔍 before you post. There are already "CodeBase" publications and "Articles" about webapi usage and JSON data handling.
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Casey Hunt:
Hi everyone,
I’m learning how MQL5 handles external web requests.
I want to understand how we can connect and fetch simple JSON data from an external API just for testing purposes (for example, weather data or demo information).
Could anyone please tell me the best method to call such data securely inside an Expert Advisor or script?
Should we use Web Requests or another approach for handling APIs in MQL5?
I really appreciate any help you can provide.
//+------------------------------------------------------------------+
//| SimpleAPIRequest.mq5 |
//| By PEDRO GOMES |
//+------------------------------------------------------------------+
#property copyright "Learning Example"
#property version "1.00"
#property script_show_inputs
input string API_URL = "put here you API URL";
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- Variables for WebRequest
string url = API_URL;
string headers = "";
string result_headers;
char post_data[];
char result[];
int timeout = 5000; // 5 seconds timeout
//--- Reset last error
ResetLastError();
//--- Make the HTTP GET request
Print("Sending request to: ", url);
int res = WebRequest(
"GET", // HTTP method
url, // URL
headers, // Request headers (empty for simple GET)
timeout, // Timeout in milliseconds
post_data, // POST data (empty for GET)
result, // Response data
result_headers // Response headers
);
//--- Check result
if(res == -1)
{
int error_code = GetLastError();
Print("WebRequest Error: ", error_code);
if(error_code == 4060)
Print("ERROR: URL not allowed. Add '", url, "' to allowed URLs in Tools->Options->Expert Advisors");
else if(error_code == 5203)
Print("ERROR: Cannot resolve URL");
else
Print("Check your internet connection and URL");
return;
}
//--- Success! Process the response
Print("HTTP Response Code: ", res);
//--- Convert result to string
string json_response = CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8);
Print("JSON Response: ", json_response);
//--- Simple JSON parsing example (manual parsing)
ParseWeatherData(json_response);
}
//+------------------------------------------------------------------+
//| Simple JSON parsing function (basic example) |
//+------------------------------------------------------------------+
void ParseWeatherData(string json)
{
Print("--- Parsing JSON Data ---");
//--- Find temperature value (simple string search)
int temp_pos = StringFind(json, "\"temperature_2m\":");
if(temp_pos >= 0)
{
int start = temp_pos + 17; // Length of "temperature_2m":
int end = StringFind(json, ",", start);
if(end < 0) end = StringFind(json, "}", start);
string temp_str = StringSubstr(json, start, end - start);
double temperature = StringToDouble(temp_str);
Print("Current Temperature: ", temperature, "°C");
}
else
{
Print("Could not find temperature in response");
}
//--- For production code, consider using a proper JSON library
//--- or more robust parsing methods
}
//+------------------------------------------------------------------+
//| Alternative: Request with headers (for APIs requiring auth) |
//+------------------------------------------------------------------+
int WebRequestWithHeaders(string url, string api_key, string &response)
{
string headers = "Content-Type: application/json\r\n";
headers += "Authorization: Bearer " + api_key + "\r\n";
string result_headers;
char post_data[];
char result[];
int timeout = 5000;
int res = WebRequest(
"GET",
url,
headers,
timeout,
post_data,
result,
result_headers
);
if(res != -1)
response = CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8);
return res;
}
//+------------------------------------------------------------------+
//| POST Request Example |
//+------------------------------------------------------------------+
int PostRequestExample(string url, string json_data, string &response)
{
string headers = "Content-Type: application/json\r\n";
string result_headers;
char post_data[];
char result[];
//--- Convert JSON string to char array
StringToCharArray(json_data, post_data, 0, WHOLE_ARRAY, CP_UTF8);
ArrayResize(post_data, ArraySize(post_data) - 1); // Remove null terminator
int res = WebRequest(
"POST",
url,
headers,
5000,
post_data,
result,
result_headers
);
if(res != -1)
response = CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8);
return res;
}
//+------------------------------------------------------------------+
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register