Sample code for 30+ languages & platforms
Lazarus Pascal

HTTP Basic Authentication Test

Demonstrates how to do HTTP basic authentication using Chilkat.

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;

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

procedure RunDemo;
var
  http: THttp;
  jsonResponse: string;

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

  http := THttp.Create;

  //  To use HTTP Basic authentication:
  http.Login := 'myLogin';
  http.Password := 'myPassword';
  http.BasicAuth := True;

  //  Run the test using this URL with the credentials above.  
  //  (Works while httpbin.org keeps the test endpoint available.)
  jsonResponse := http.QuickGetStr('https://httpbin.org/basic-auth/myLogin/myPassword');
  if (http.LastMethodSuccess = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('Response status code: ' + http.LastStatus);

  WriteLn(jsonResponse);

  //  Output:

  //  Response status code: 200
  //  {
  //    "authenticated": true, 
  //    "user": "myLogin"
  //  }


  http.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.