Sample code for 30+ languages & platforms
Lazarus Pascal

QuickGetBd Example

The QuickGetBd method is called to send a GET request to download a binary target, such as PDF, zip, image file, etc. into a Chilkat BinData object.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
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.Http,
  Chilkat.BinData,
  Chilkat.Pdf;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  bd: TBinData;
  statusCode: Integer;
  pdf: TPdf;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;
  http.KeepResponseBody := True;

  bd := TBinData.Create;

  //  Download the contents of a PDF file into the bd object.
  success := http.QuickGetBd('https://www.chilkatsoft.com/hello.pdf',bd);
  if (success = False) then
    begin
      //  This will happen if the response status code was 400 or greater,
      //  or if the request could not be sent, or if no response was received.
      //  
      //  In other words, success will only be True if bd contains the desired content at the URL (not an error response).
      statusCode := http.LastStatus;
      WriteLn('Response status: ' + statusCode);

      if (statusCode = 0) then
        begin
          //  There was an error in communications.  
          WriteLn(http.LastErrorText);
        end
      else
        begin
          //  We received a response status code indicating failure.
          //  Examine the response body.
          WriteLn(http.LastResponseBody);
        end;

      Exit;
    end;

  //  Load the downloaded PDF into a Chilkat PDF object.
  pdf := TPdf.Create;
  success := pdf.LoadBd(bd);
  if (success = False) then
    begin
      WriteLn(pdf.LastErrorText);
      Exit;
    end;

  //  ...

  WriteLn('Success.');


  http.Free;
  bd.Free;
  pdf.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.