Sample code for 30+ languages & platforms
Delphi DLL

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 DLL Downloads

Delphi DLL
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
bAutoReconnect: Boolean;
statusCode: Integer;
numHeaders: Integer;
i: Integer;
name: PWideChar;
value: PWideChar;

begin
success := False;

rest := CkRest_Create();
bTls := True;
bAutoReconnect := True;
success := CkRest_Connect(rest,'example.com',443,bTls,bAutoReconnect);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

success := CkRest_SendReqNoBody(rest,'GET','/api/items/123');
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

statusCode := CkRest_ReadResponseHeader(rest);
if (statusCode < 0) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    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 := CkRest_getNumResponseHeaders(rest);

for i := 0 to numHeaders - 1 do
  begin
    name := CkRest__responseHdrName(rest,i);
    if (CkRest_getLastMethodSuccess(rest) = False) then
      begin
        Memo1.Lines.Add(CkRest__lastErrorText(rest));
        Exit;
      end;
    value := CkRest__responseHdrValue(rest,i);
    if (CkRest_getLastMethodSuccess(rest) = False) then
      begin
        Memo1.Lines.Add(CkRest__lastErrorText(rest));
        Exit;
      end;
    Memo1.Lines.Add(name + ': ' + value);
  end;

CkRest_Dispose(rest);