Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get the JWS Payload into BinData
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.GetPayloadBd, which writes the payload of a loaded JWS as raw bytes into a BinData.
Background. This is used when the payload is binary. In practice, validate the signature before trusting the payload.
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.BinData,
Chilkat.Jws;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
jws: TJws;
jwsCompact: string;
bdPayload: TBinData;
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 raw bytes into a BinData.
bdPayload := TBinData.Create;
success := jws.GetPayloadBd(bdPayload);
if (success = False) then
begin
WriteLn(jws.LastErrorText);
Exit;
end;
WriteLn('Payload is ' + bdPayload.NumBytes + ' bytes.');
jws.Free;
bdPayload.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.