Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

HTTP POST with Binary Data in Request Body

See more HTTP Examples

Do an HTTPS POST with a binary request body.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.FileAccess,
  Chilkat.HttpResponse;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  fac: TFileAccess;
  reqBody: TBytes;
  resp: THttpResponse;
  responseStatusCode: Integer;

begin
  success := False;

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

  http := THttp.Create;

  fac := TFileAccess.Create;

  reqBody := fac.ReadEntireFile('qa_data/pdf/helloWorld.pdf');

  resp := THttpResponse.Create;
  success := http.HttpBinary('POST','https://example.com/something',reqBody,'application/pdf',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  responseStatusCode := resp.StatusCode;
  WriteLn('Status code: ' + responseStatusCode);

  //  For example, if the response is XML, JSON, HTML, etc.
  WriteLn('response body:');
  WriteLn(resp.BodyStr);


  http.Free;
  fac.Free;
  resp.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.