Sample code for 30+ languages & platforms
Zig

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const jws = try chilkat.Jws.init();
    defer jws.deinit();

    // Load the JWS compact serialization.
    const jws_compact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>";
    jws.loadJws(jws_compact) catch {
        std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
        return;
    };

    // 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.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    jws.getUnprotectedH(0, json) catch {
        std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
        return;
    };

    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});
}