Download MetaTrader 5

Does WebRequest function support HTTP DELETE?

To add comments, please log in or register
Haibo Liu
19
Haibo Liu  

I have HTTP Delete method API call via WebRequest, but it reported an error with "Error in WebRequest. Error code  =4051". It works while I use POST/GET method. Below is my code.


call point:

DeleteOrders("26041");



int DeleteOrders(string OrderNo = "all") {

   string url = "http://localhost/orders";

   if (StringCompare(OrderNo,"all") != 0) {

      url += "/" + OrderNo;

   }

   Print(url);

   //string response = ServiceCall(url,"DELETE",NULL);

   string cookie=NULL,headers;

   char post[],result[];

   int res;

   string response;

   res=WebRequest("DELETE",url,cookie,NULL,5000,post,0,result,headers);

   if(res==-1)

     {

      Print("Error in WebRequest. Error code  =",GetLastError());

      //--- Perhaps the URL is not listed, display a message about the necessity to add the address

      MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);

     }

   else

     {

      response = CharArrayToString(result,0,WHOLE_ARRAY,CP_UTF8);

     }

   Print(response);

   return 0;

}

Can someone give some comments? thanks in advance. 

Sergey Golubev
Moderator
101557
Sergey Golubev  

Hello,

Please use the SRC button when you post code. Thank you.

Haibo Liu
19
Haibo Liu  
Sergey Golubev:

Hello,

Please use the SRC button when you post code. Thank you.

Updated.

Thanks 

sinall
6
sinall  

I have the same question. Does 'WebRequest' support method 'DELETE'? Can anyone clarify this? Thanks.

We need to call 'DELETE' method against the ordering system to cancel an open order.

Alain Verleyen
Moderator
35040
Alain Verleyen  
sinall:

I have the same question. Does 'WebRequest' support method 'DELETE'? Can anyone clarify this? Thanks.

We need to call 'DELETE' method against the ordering system to cancel an open order.

Not supported currently.
sinall
6
sinall  

Thanks for your clarification. 

The 3rd-party ordering API is currently designed only accept 'DELETE' method for order deletion. (This does make sense

Could you please suggest a workaround for this?

Or, does MQL4 support calling a DLL or Python script? We then could workaround it.

Thanks.

Alain Verleyen
Moderator
35040
Alain Verleyen  
sinall:

Thanks for your clarification. 

The 3rd-party ordering API is currently designed only accept 'DELETE' method for order deletion. (This does make sense

Could you please suggest a workaround for this?

Or, does MQL4 support calling a DLL or Python script? We then could workaround it.

Thanks.

You can use WINAPI (Windows DLL). Please search on this site, there is a lot of resource on that subject.
Haibo Liu
19
Haibo Liu  
sinall:

Thanks for your clarification. 

The 3rd-party ordering API is currently designed only accept 'DELETE' method for order deletion. (This does make sense

Could you please suggest a workaround for this?

Or, does MQL4 support calling a DLL or Python script? We then could workaround it.

Thanks.

 

Thanks, Alain. Finally I implemented the DELETE as below, hope it is useful for others:

 

bool HttpDELETE(string host, string script, string& strWebPage)
{
  int hIntrn = InternetOpenW(AGENT, INTERNET_OPEN_TYPE_DIRECT, "0", "0", 0);
  if (hIntrn == 0) {
    return (false);
  }

  int hConn = InternetConnectW(hIntrn,
                              host,
                              INTERNET_DEFAULT_HTTP_PORT,
                              NULL,
                              NULL,
                              INTERNET_SERVICE_HTTP,
                              0,
                              NULL);

  if (hConn == 0) {
    return (false);
  }

  int dwOpenRequestFlags =   // _IGNORE_REDIRECT_TO_HTTP |
                             // _IGNORE_REDIRECT_TO_HTTPS |
                             // INTERNET_FLAG_KEEP_CONNECTION |
                             // INTERNET_FLAG_NO_AUTO_REDIRECT |
                             // INTERNET_FLAG_NO_COOKIES |
                             // INTERNET_FLAG_NO_CACHE_WRITE |
                             INTERNET_FLAG_NO_UI
                             ;

  string accept[];

  int hReq = HttpOpenRequestW(hConn,
                             "DELETE",
                             script,
                             "",
                             NULL,
                             accept,
                             dwOpenRequestFlags,
                             NULL);

  
  int request[];
  if (!HttpSendRequestW(hReq, "", 0, request, 0)) {
    //Print(GetLastError());
    return (false);
  }

  // Read response

  int     lReturn[]  = {1};
  string  sBuffer    = "";

  while (TRUE) {
    if (InternetReadFile(hReq, sBuffer, BUFSIZ, lReturn) <= 0 || lReturn[0] == 0) {
      break;
    }
    strWebPage = StringConcatenate(strWebPage, StringSubstr(sBuffer, 0, lReturn[0]));
  }

  InternetCloseHandle(hReq);
  InternetCloseHandle(hConn);
  InternetCloseHandle(hIntrn);

  return (true);
}
To add comments, please log in or register