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

Get a JWS Unprotected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetUnprotectedH, which copies the optional unprotected header of a signature into a JsonObject.

Background. The unprotected header is present only in JSON serialization forms that carry one. It is not covered by the signature.

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

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

procedure RunDemo;
var
  success: Boolean;
  jws: TJws;
  jwsCompact: string;
  json: TJsonObject;

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;

  //  Copy the optional unprotected header of signature 0 into a JsonObject.  This applies to JWS in a
  //  JSON serialization form that carries an unprotected header.
  json := TJsonObject.Create;
  success := jws.GetUnprotectedH(0,json);
  if (success = False) then
    begin
      WriteLn(jws.LastErrorText);
      Exit;
    end;

  json.EmitCompact := False;
  WriteLn(json.Emit());


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