Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
REST File Upload (multipart/form-data)
See more REST Examples
Demonstrates how to upload a file using multipart/form-data.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.Stream,
Chilkat.Rest;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
rest: TRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
fileStream: TStream;
responseBody: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest := TRest.Create;
// Connect to the HTTP server using TLS.
bTls := True;
port := 443;
bAutoReconnect := True;
// Make sure to replace "www.chilkatsoft.com" with your domain..
success := rest.Connect('www.chilkatsoft.com',port,bTls,bAutoReconnect);
if (success <> True) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
fileStream := TStream.Create;
fileStream.SourceFile := 'qa_data/jpg/starfish.jpg';
rest.AddHeader('Content-Type','multipart/form-data');
rest.PartSelector := '1';
rest.AddHeader('Content-Type','image/jpg');
rest.AddHeader('Content-Disposition','form-data; name="filename"; filename="starfish.jpg"');
rest.SetMultipartBodyStream(fileStream);
rest.PartSelector := '0';
responseBody := rest.FullRequestMultipart('POST','/the_uri_path_to_receive_the_upload');
if (rest.LastMethodSuccess <> True) then
begin
WriteLn(rest.LastErrorText);
Exit;
end;
if (rest.ResponseStatusCode <> 200) then
begin
WriteLn('Received error response code: ' + rest.ResponseStatusCode);
Exit;
end;
WriteLn('File uploaded');
rest.Free;
fileStream.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.