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

Get the JWS Payload into a StringBuilder

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetPayloadSb, which writes the payload of a loaded JWS as text into a StringBuilder, decoded using the given charset.

Background. In practice, validate the signature before trusting the payload.

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.Jws,
  Chilkat.StringBuilder;

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

procedure RunDemo;
var
  success: Boolean;
  jws: TJws;
  jwsCompact: string;
  sbPayload: TStringBuilder;

begin
  success := False;

  jws := TJws.Create;

  //  Load the JWS compact serialization.
  jwsCompact := 'eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>';
  success := jws.LoadJws(jwsCompact);
  if (success = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;

  //  Get the JWS payload as text into a StringBuilder, decoded using the given charset.
  sbPayload := TStringBuilder.Create;
  success := jws.GetPayloadSb('utf-8',sbPayload);
  if (success = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;
  WriteLn(sbPayload.GetAsString());


  jws.Free;
  sbPayload.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.