Sample code for 30+ languages & platforms
Delphi ActiveX

Enumerate REST Response Header Names

See more REST Examples

Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
rest: TChilkatRest;
bTls: Integer;
bAutoReconnect: Integer;
statusCode: Integer;
numHeaders: Integer;
i: Integer;
name: WideString;
value: WideString;

begin
success := 0;

rest := TChilkatRest.Create(Self);
bTls := 1;
bAutoReconnect := 1;
success := rest.Connect('example.com',443,bTls,bAutoReconnect);
if (success = 0) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

success := rest.SendReqNoBody('GET','/api/items/123');
if (success = 0) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

statusCode := rest.ReadResponseHeader();
if (statusCode < 0) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

//  Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
//  and ResponseHdrValue returns the value at the same index.
numHeaders := rest.NumResponseHeaders;

for i := 0 to numHeaders - 1 do
  begin
    name := rest.ResponseHdrName(i);
    if (rest.LastMethodSuccess = 0) then
      begin
        Memo1.Lines.Add(rest.LastErrorText);
        Exit;
      end;
    value := rest.ResponseHdrValue(i);
    if (rest.LastMethodSuccess = 0) then
      begin
        Memo1.Lines.Add(rest.LastErrorText);
        Exit;
      end;
    Memo1.Lines.Add(name + ': ' + value);
  end;