How to send a DELETE request with Indy?

。_饼干妹妹 提交于 2019-12-10 23:45:23

问题


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.


回答1:


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, []);



回答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;


来源:https://stackoverflow.com/questions/21002337/how-to-send-a-delete-request-with-indy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!