Sample code for 30+ languages & platforms
Lazarus Pascal

Add a Content-Length Header to MIME

See more MIME Examples

Demonstrates the Chilkat Mime.AddContentLength method, which computes the byte length of the entity's serialized body and adds or updates its Content-Length header. It takes no arguments and is a void method. The count is based on the transfer-encoded serialized body.

Note: The file path is relative to the application's current working directory. Absolute paths may also be used. Supply the path to your own file.

Background: Some protocols and consumers want a Content-Length declaring the body size up front. This computes it correctly from the encoded body — the bytes actually written — rather than the decoded content, and updates any existing header so it stays accurate. Call it after the body is final; if you change the body afterward, call it again, or remove the header, since a stale length is worse than none.

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.Mime;

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

procedure RunDemo;
var
  success: Boolean;
  mime: TMime;
  contentLength: string;

begin
  success := False;

  mime := TMime.Create;
  success := mime.LoadMimeFile('qa_data/message.eml');
  if (success = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;

  //  Compute the byte length of the entity's serialized body and add (or update) its Content-Length
  //  header.  The count is based on the transfer-encoded serialized body.  It is a void method.
  mime.AddContentLength();

  contentLength := mime.GetHeaderField('Content-Length');
  if (mime.LastMethodSuccess = False) then
    begin
      WriteLn(mime.LastErrorText);
      Exit;
    end;
  WriteLn('Content-Length: ' + contentLength);


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