Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Send a REST Request using StringBuilder Bodies
See more REST Examples
Demonstrates Rest.FullRequestSb, which sends a complete request whose text body is read from a StringBuilder and stores the text response body in a second StringBuilder.
Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.
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;
sbRequest: TStringBuilder;
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;
// FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
// response body in a second StringBuilder. This avoids extra string conversions for large bodies.
sbRequest := TStringBuilder.Create;
sbRequest.Append('{ "query": "chilkat" }');
rest.AddHeader('Content-Type','application/json');
sbResponse := TStringBuilder.Create;
success := rest.FullRequestSb('POST','/api/search',sbRequest,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;
sbRequest.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.