Sample code for 30+ languages & platforms
Delphi DLL

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
bAutoReconnect: Boolean;
statusCode: Integer;
numHeaders: Integer;
i: Integer;
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;

//  Return the value of the response header at a zero-based index.  Use the same index with
//  ResponseHdrName to get the corresponding field name.
numHeaders := CkRest_getNumResponseHeaders(rest);

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

CkRest_Dispose(rest);