Unable to send Headers, and data to API via POST method using WebRequest

 

Hi all, I am trying to send WebRequest in MQL5 to API via POST method. The request received at API end however, headers and data dont.  following is my code:


   // JSON text to send
   string strJsonText = "{\r\n\"key\": \"value\"\r\n}";
   
   // Text must be converted to a uchar array. Note that StringToCharArray() adds 
   // a nul character to the end of the array unless the size/length parameter
   // is explicitly specified 
   uchar jsonData[];
   //StringToCharArray(strJsonText, jsonData, 0, StringLen(strJsonText));

    StringToCharArray(strJsonText,jsonData,0,StringLen(strJsonText),CP_UTF8);
    
   // Use MT5's WebRequest() to send the JSON data to the server.
   char serverResult[];
   string serverHeaders = "Content-Type: application/json";
   
   
   serverHeaders = "Content-Type: application/json; charset=utf-8\r\nExpect: 100-continue\r\nConnection: Keep-Alive\r\n";


   
   int res = WebRequest("POST", "http://127.0.0.1:5000/Predict", "", "", 10000, jsonData, ArraySize(jsonData), serverResult, serverHeaders);

   Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));
   


following is the snapshot of request object in API.



Please help

 
did you allowed mt to use web request through tools > options > expert advisor
you also need to specify the url you want to send webrequest to.

also i suggest using meta traders own type of request which is available on website and in mt help (press F1).
because its own version may be less tricky while its not even uses json for post values and puts them in a string.

also please use some error handling to get the error code which will help you to get error codes and find out what really causes that error. (for example 4060 means webRequest no send mostly because use did not allowed it in optinos and 5203 means the request sent but response is not ...) - you can also find these codes and their meaning in the help menu.
 
yes it is allowed, my request is getting in to the WebService/API, I checked in debug mode. The issue is that I am not receiving Headers, and Data/JSON over there.
 
Thanks for your help, I manages to get the issue resolved. 
 

And what was the problem and what the solution?

Would be cool to make this thread a nice lesson for others rather than a dead end.

 

Marcel Fitzner, probably the problem was that the OP was using the wrong WebRequest to send the headers to the server, if you look at the docs, to send headers to the server you need to send the request like this 

// JSON text to send
   string strJsonText = "{\r\n\"key\": \"value\"\r\n}";
   
   // Text must be converted to a uchar array. Note that StringToCharArray() adds 
   // a nul character to the end of the array unless the size/length parameter
   // is explicitly specified 
   uchar jsonData[];
   //StringToCharArray(strJsonText, jsonData, 0, StringLen(strJsonText));

    StringToCharArray(strJsonText,jsonData,0,StringLen(strJsonText),CP_UTF8);
    
   // Use MT5's WebRequest() to send the JSON data to the server.
   char serverResult[];
   string serverHeaders;
   
   
   string requestHeaders = "Content-Type: application/json; charset=utf-8\r\nExpect: 100-continue\r\nConnection: Keep-Alive";


   
   int res = WebRequest("POST", "http://127.0.0.1:5000/Predict", requestHeaders, 10000, jsonData, serverResult, serverHeaders);

   Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));
Documentação sobre MQL5: Funções de rede / WebRequest
Documentação sobre MQL5: Funções de rede / WebRequest
  • www.mql5.com
WebRequest - Funções de rede - Referência MQL5 - Referência sobre algorítimo/automatização de negociação na linguagem para MetaTrader 5
Reason: