Search code examples
delphihttpindyindy10

How to send a DELETE request with Indy?


I tried several search for examples on the web on how to send a DELETE request with TidHTTP without success...

I'm working with Delphi Indy 10.0.52. Apparently there is not such a method as Delete in the TIdHTTP object. Is it possible? I'm missing something here... Help!

I ended up with something (not working) like this:

procedure TRestObject.doDelete( deleteURL:String );
var
  res : TStringStream;
begin
  res := TStringStream.Create('');
  http := TidHTTP creation and init...
  try
    http.Request.Method := IdHTTP.hmDelete;
    http.Put( deleteURL, res );
  except
    on E: EIdException do
      showmessage('Exception (class '+ E.ClassName +'): ' + E.Message);
    on E: EIdHTTPProtocolException do
      showmessage('Protocol Exception (HTTP status '+ IntToStr(E.ErrorCode) +'): ' + E.Message);
    on E: EIdSocketError do
      showmessage('Socket Error ('+ IntToStr(E.LastError) +'): ' + E.Message);
  end;
  res.Free;
end;

This is a method in an object already handling GET, PUT, POST to a RESTful web service implemented with django-tastypie. I have all permissions and authentications set in the object's init phase.


Solution

  • As its name suggests, TIdHTTP.Put() forces the request method to PUT. So you cannot use it to send other requests.

    10.0.52 is a very old version, you really should upgrade to the latest 10.6.0, which has a TIdHTTP.Delete() method:

    http.Delete(deleteURL, res);
    

    If that is not an option, then to send a custom request with 10.0.52, you will have to call TIdHTTP.DoRequest() instead. However, DoRequest() is declared as protected so you will have to use an accessor class to call it, eg:

    type
      TIdHTTPAccess = class(TIdHTTP)
      end;
    

    TIdHTTPAccess(http).DoRequest('DELETE', deleteURL, nil, res, []);