Does WebRequest function support HTTP DELETE?

 

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. 

 

Hello,

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

 
Sergey Golubev:

Hello,

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

Updated.

Thanks 

 

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.

 
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.
 

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.

 
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.
 
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);
}
 
Does it really work? What .mqh file do you use?
 

Yes it is possible, just set the httpType to "POST" then write send this as Header : 

X-HTTP-Method-Override:DELETE

You Can also send PATCH or DELETE with this approach.


Example:

string response = SendResquest("POST", "https://google.com", body, "X-HTTP-Method-Override:DELETE", 5000);


string SendResquest(string httpType, string address, string bodyData = "", string headers = "", int timeout=5000)
{
    uchar bodyDataCharArray[];
    uchar resultDataCharArray[];
    string resultHeader;
    ArrayResize(bodyDataCharArray, StringToCharArray(bodyData, bodyDataCharArray)-1);

    int response = WebRequest(httpType, address, headers, timeout, bodyDataCharArray, resultDataCharArray, resultHeader);

    string result = CharArrayToString(resultDataCharArray); 

    if(response == 200)
        return result;
    Print("Error when trying to call API : ", GetLastError());
    return "";
}


Thanks to Mohammad Hasan Daneshvar to tell me this.

Reason: