Please show your MQL code.
The MQL code is very simple.
The only question is why the response comes with Content-Type: text/html; charset=UTF-8, if Accept: application/json is set in the request.
string url = "http://localhost:8000/api/user"; string request_headers = "Accept: application/json"; uchar request_data[]; uchar response_body[]; string response_headers; int response_code = -1; ResetLastError(); response_code = WebRequest("GET", url, request_headers, NULL, request_data, response_body, response_headers); int error = GetLastError(); Print("\nresponse_code: ", response_code, ", error: ", error); Print("[response_headers]"); Print(response_headers);
//---
response_code: 500, error: 0 [response_headers] Host: localhost Date: Thu, 20 Jan 2022 16:25:15 GMT Connection: close X-Powered-By: PHP/7.4.6 Cache-Control: no-cache, private date: Thu, 20 Jan 2022 16:25:15 GMT Content-Type: text/html; charset=UTF-8 Vary: Origin
We make a request without any headers and get this result. In this case everything is correct and should be so. The answer is returned in text/html format.
If you change the requested data type to application/json, you get the following result:
//---
We always get the response when sending request from MetaTrader 5 with Accept: application/json header:
//---
How can I get the expected result?
Everything works as expected on the command line, in Postman and in the browser application.
Hi Anatoli,
I reported this issue years ago, I never got a reply from Metaquotes.
Thank you for the information.
Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий
Alain Verleyen, 2019.04.16 03:50
Build 2007/2025.
Кажется, невозможно изменить заголовок «Accept» в WebRequest (с mql4 это работает).
Прикрепленный код должен возвращать JSON-ответ, например:
{ "responseParameters": { "doui_setResponseParameters": [ "8AA8D0CD6A05153A016A16735AF94E0E"]}}
Но ответ - HTML-файл, потому что серверу нужен заголовок «Accept: application / json». Я знаю это, потому что это работает с Почтальоном, например.
При проверке HTTP, отправляемого MT5 с Wireshark, заголовок «Принять» всегда:
Принять: * / *
Это ошибка или я что-то упустил?
All other headers in my tests are accepted by the server normally with \r\n.
string CProgram::GetHeaders(void) { string eol = "\r\n"; string headers = ""; headers += (m_xsrf_token != ""? "X-XSRF-TOKEN: " + m_xsrf_token + eol : ""); headers += (m_cookie != ""? m_cookie + eol : ""); headers += "Accept: application/json" + eol; headers += "Referer: localhost:3000"; return headers; }
The bug only appears with Accept: application/json. MetaTrader 5 replaces it with Accept: */*.
You can verify this with Wireshark:
All other headers in my tests are accepted by the server normally with \r\n.
The bug only appears with Accept: application/json. MetaTrader 5 replaces it with Accept: */*.
You can verify this with Wireshark:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
We make a request without any headers and get this result. In this case everything is correct and should be so. The answer is returned in text/html format.
If you change the requested data type to application/json, you get the following result:
//---
We always get the response when sending request from MetaTrader 5 with Accept: application/json header:
Content-Type: text/html; charset=UTF-8
//---
How can I get the expected result?
Everything works as expected on the command line, in Postman and in the browser application.