Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Send a REST Request with a Text Body
See more REST Examples
Demonstrates Rest.FullRequestString, a convenience method that sends a complete request whose text body (such as JSON, XML, or plain text) is supplied directly, then reads and returns the response body as text.
Background. The full-request convenience methods perform the entire request/response exchange in a single call. FullRequestString is well suited to typical JSON or XML API calls where the body fits comfortably in a string.
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;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
bAutoReconnect: Boolean;
jsonBody: string;
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;
// Send a complete request whose text body is supplied directly, such as JSON. The response body is
// read as text and returned. The 1st argument is the HTTP verb, the 2nd is the URI path, and the
// 3rd is the request body.
rest.AddHeader('Content-Type','application/json');
jsonBody := '{ "name": "example", "active": true }';
responseText := rest.FullRequestString('POST','/api/items',jsonBody);
if (rest.LastMethodSuccess = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
WriteLn(responseText);
rest.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.