Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
URL-Encode a MIME Body
See more MIME Examples
Demonstrates the Chilkat Mime.UrlEncodeBody method, which replaces the current body with an application/x-www-form-urlencoded-style representation using the named charset. The only argument is the charset; it is a void method.
Background: This transforms the body into the form-encoding used by HTML form submissions — spaces to
+, reserved characters to percent-escapes — interpreting the text through the given charset first so non-ASCII characters encode correctly. It is a niche operation for constructing form-style payloads within a MIME part.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.Mime;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
mime: TMime;
encodedBody: string;
begin
success := False;
// Demonstrates the Mime.UrlEncodeBody method, which replaces the current body with an
// application/x-www-form-urlencoded representation using the named charset. The only argument is
// the charset. It is a void method.
mime := TMime.Create;
success := mime.SetBodyFromPlainText('name=John Doe & city=New York');
if (success = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
// URL-encode the body: spaces become "+", reserved characters become %-escapes.
mime.UrlEncodeBody('utf-8');
encodedBody := mime.GetBodyDecoded();
if (mime.LastMethodSuccess = False) then
begin
WriteLn(mime.LastErrorText);
Exit;
end;
WriteLn(encodedBody);
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.