Sample code for 30+ languages & platforms
Zig

Client Certificate in REST (USB Token or Smartcard)

See more REST Examples

Demonstrates how to use a client certificate with a REST connection where the certificate and private key are located on a USB token or smart card.

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example shows how to use the Chilkat socket object's connection.
    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    const socket = try chilkat.Socket.init();
    defer socket.deinit();

    // Set the certificate to be used for mutual TLS authentication
    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    // If the smartcard or token requires a PIN...
    cert.setSmartCardPin("000000");

    cert.loadFromSmartcard("") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    socket.setSslClientCert(cert) catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    // Establish the connection using the socket object (with client certificate authentication).
    const b_tls = true;
    const port = 443;
    const max_wait_ms = 5000;
    socket.connect("www.example.com", port, b_tls, max_wait_ms) catch {
        std.debug.print("Connect Failure Error Code: {d}\n", .{socket.getConnectFailReason()});
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    const b_auto_reconnect = true;

    // Use the connection:
    rest.useConnection(socket, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // At this point we are connected and can make REST calls...
    // For example..
    _ = rest.fullRequestNoBody(alloc, "GET", "/someQuery") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };
}