вівторок, 6 жовтня 2020 р.

Indy HTTP DELETE request with a body

Below is a code shows how to send DELETE request with a body. So an input body stream could consists with the JSON\XML or whatever you want.

type
  TIdHTTPAccess = class(TIdHTTP)
  end;

...
var
  IdHTTP: TIdHTTP;
...

function DELETE_HTTP(AURL, ABody: String): String;
var
  bodyStream: TStringStream;
  responseStream: TStringStream;
begin
  Result := '';
  IdHTTP.Request.ContentType := 'application/xml';
  IdHTTP.Request.AcceptCharSet := 'utf-8';
  responseStream := TStringStream.Create('', TEncoding.UTF8);
  try
    bodyStream := TStringStream.Create(ABody, TEncoding.UTF8);
    try
      TIdHTTPAccess(IdHTTP).DoRequest('DELETE', AURL, bodyStream, responseStream, []);
      if
        Assigned(responseStream)
        and(responseStream.DataString.Length > 0)
      then
      begin
        Result := responseStream.DataString;
      end;
    finally
      FreeAndNil(bodyStream);
    end;
  finally
    FreeAndNil(responseStream);
  end;
end;