8

Indy's TIdHttp class has - in a recent version - a Delete() routine. But for some reason, this is a procedure, not a function. Put(), Get(), etc. are all functions that return the content of the response body. Either as a string or have it delivered to a TStream. This is not possible with Delete(), which is contradictory to DELETE's definition:

A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.

Source.

I then tried using GetResponse(), but that instead simply closed my connection gracefully, without filling in the response.

So how do I read the contents of the response body from a DELETE response?

Svip
  • 2,958
  • 3
  • 22
  • 33
  • How about `TIdHttp.ResponseText` property? in any case what do you expect to find in the response text? (other than testing for the `ResponseCode`) – kobik Jan 08 '15 at 13:17
  • `TIdHttp.ResponseText` was apparently not what I thought. It is closely related to `ResponseCode`, so on a 200 response, `TIdHttp.ResponseText` contains 'HTTP/1.1 200 OK', not the actually response body. For now, my work around can work without the data, but it was just returning the ID of the item it deleted, so I could verify it deleted the right item. – Svip Jan 08 '15 at 13:41
  • I have never used `DELETE` method, but I guess You could return that ID in the response headers. Also, I'm assuming you control the server, and I'm not so sure why you need to use `DELETE` method in the first place. why not use `GET` or `POST` and indicate in the request parameters which resource to delete? – kobik Jan 08 '15 at 14:00
  • 1
    Because this is a RESTful API, and therefore it makes more sense to use a DELETE method. HTTP methods exists and are a known standard, why not use them? – Svip Jan 08 '15 at 14:39

1 Answers1

8

You need to update your Indy installation. Overloads for getting DELETE method responses were added in September 2013 in SVN revision 5056 for this feature request:

Add overloads for TIdHTTP Delete() and Options() methods that can output a response's message body

If you don't want to update Indy for some reason, you can subclass the TIdHTTP component and add a method that will pass a response stream to the DoRequest method, e.g.:

type
  TIdHTTP = class(IdHTTP.TIdHTTP)
  public
    procedure DeleteEx(AURL: string; AResponseContent: TStream);
  end;

implementation

{ TIdHTTP }

procedure TIdHTTP.DeleteEx(AURL: string; AResponseContent: TStream);
begin
  DoRequest(Id_HTTPMethodDelete, AURL, nil, AResponseContent, []);
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
TLama
  • 75,147
  • 17
  • 214
  • 392