Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Capture the Composed REST Request for Debugging
See more REST Examples
Demonstrates Rest.GetLastDebugRequest, which copies the fully composed HTTP request (request line, headers, and body) into a BinData. The example builds a two-part multipart request so the captured output shows the multipart boundaries, part headers, and part bodies. This works when the DebugMode property was enabled while the request was composed.
Background. When DebugMode is enabled, Chilkat builds the request but does not transmit it, so GetLastDebugRequest can reveal the exact bytes that would be sent. Inspecting the composed request is valuable for diagnosing API problems.
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.BinData,
Chilkat.Rest;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
bAutoReconnect: Boolean;
bdRequest: TBinData;
requestText: 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;
// Enable debug mode so the fully composed HTTP request is captured instead of being sent.
rest.DebugMode := True;
// Build a multipart request with two parts. The captured debug output will show the multipart
// boundaries, part headers, and part bodies.
rest.PartSelector := '1';
rest.AddHeader('Content-Type','text/plain');
rest.AddHeader('Content-Disposition','form-data; name="field1"');
success := rest.SetMultipartBodyString('value1');
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
rest.PartSelector := '2';
rest.AddHeader('Content-Type','application/json');
rest.AddHeader('Content-Disposition','form-data; name="metadata"');
success := rest.SetMultipartBodyString('{ "active": true }');
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
// Compose the multipart request. In debug mode the request is built but not transmitted.
success := rest.SendReqMultipart('POST','/api/upload');
if (success = False) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
// Copy the composed request (request line, headers, and multipart body that would be sent) into a
// BinData, then view it as text.
bdRequest := TBinData.Create;
rest.GetLastDebugRequest(bdRequest);
requestText := bdRequest.GetString('utf-8');
if (bdRequest.LastMethodSuccess = False) then
begin
WriteLn(bdRequest.LastErrorText);
Exit;
end;
WriteLn(requestText);
rest.Free;
bdRequest.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.