2

I need to do a call with TIDHTTP by specifying a custom HTTP method.

In postman I achieve this by typing the method name in the dropdown where I can choose between GET/POST/PUT/...:

custom method in postman

how to achieve this in TIdHttp? In this example the HTTP method is not GET but GO (GO of course is custom and not standard).

Thanks.

UnDiUdin
  • 14,924
  • 39
  • 151
  • 249

1 Answers1

3

TIdHTTP has a public Request.Method property, which you might be tempted to use, however that won't work, because the TIdCustomHTTP.DoRequest() method (which does all of the actual work) overwrites that property with its own AMethod parameter. So, you will have to call DoRequest() directly instead:

procedure DoRequest(const AMethod: TIdHTTPMethod; AURL: string;
  ASource, AResponseContent: TStream; AIgnoreReplies: array of Int16); virtual;

TIdHTTPMethod is just a string, so you can pass in any method name you want.

However, DoRequest() is declared as protected in TIdCustomHTTP 1, so you will have to use a descendant/accessor class to reach it, eg:

type
  TIdHTTPAccess = class(TIdHTTP)
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TIdHTTPAccess(IdHTTP1).DoRequest('GO', 'http://127.0.0.1:1308/...', ParamsAsNeeded...);
end;

Or, you can use a class helper instead (Delphi 2006+ only), eg:

type
  TIdHTTPHelper = class helper for TIdHTTP
    procedure Go(AURL: String; ParamsAsNeeded...);
  end;

procedure TIdHTTPHelper.Go(AURL: String; ParamsAsNeeded...);
begin
  DoRequest('GO', AURL, ParamsAsNeeded...);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  IdHTTP1.Go('http://127.0.0.1:1308/...', ParamsAsNeeded...);
end;

1: There is an open ticket in Indy's issue tracker about this: #254 Make TIdHTTP.DoRequest() public.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770