2

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.

Dom
  • 70
  • 1
  • 9
  • `TIdHTTP` class has the `Delete` method. – TLama Jan 08 '14 at 17:23
  • But I constantly get an error from the compiler saying there's not... – Dom Jan 08 '14 at 17:29
  • 10.0.52 is a very old version, I don't recall if it had the `Delete()` method yet. Newer versions certainly do. You really should upgrade. – Remy Lebeau Jan 08 '14 at 17:32
  • There may also be (or be omitted ) Delete in other Http libs than Indy – Arioch 'The Jan 08 '14 at 18:45
  • OT: your exception handling chain is wrong. Since the `EIdHTTPProtocolException` is a (non-direct) descendant of the `EIdException` exception class, it should be above the `EIdException` handler in the exception handling chain. The same applies to the `EIdSocketError` which is a descendant of the `EIdException` exception class and also should be above the `EIdException` handler. Generally, first should be specific exception handlers and then their ancestors (`Exception` should be last, if present). Currently both specific exceptions you've handled in your code ended up on the first handler. – TLama Jan 08 '14 at 23:21

2 Answers2

6

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, []);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

You can check this delphi rest client https://github.com/fabriciocolombo/delphi-rest-client-api

Look in file HttpConnectionIndy.pas how is delete implemented.

procedure TIdHTTP.Delete(AURL: string);
begin
  try
    DoRequest(Id_HTTPMethodDelete, AURL, Request.Source, nil, []);
  except
    on E: EIdHTTPProtocolException do
    raise EHTTPError.Create(e.Message, e.ErrorMessage, e.ErrorCode);
  end;
end;