Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Send a Bodyless REST Request into a StringBuilder
See more REST Examples
Demonstrates Rest.FullRequestNoBodySb, which sends a request with no body and stores the text response body in a StringBuilder.
Background. This variant of the no-body full request writes the response into a StringBuilder rather than returning it as a string, which is convenient when the response will be further processed in place.
Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Rest,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
bAutoReconnect: Boolean;
sbResponse: TStringBuilder;
responseText: string;
begin
success := False;
rest := TRest.Create;
bTls := True;
bAutoReconnect := True;
success := rest.Connect('example.com',443,bTls,bAutoReconnect);
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
// FullRequestNoBodySb sends a request with no body and stores the text response body in a
// StringBuilder.
sbResponse := TStringBuilder.Create;
success := rest.FullRequestNoBodySb('GET','/api/items/123',sbResponse);
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
responseText := sbResponse.GetAsString();
if (sbResponse.LastMethodSuccess = False) then
begin
WriteLn(sbResponse.LastErrorText);
Exit;
end;
WriteLn(responseText);
rest.Free;
sbResponse.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.