Sample code for 30+ languages & platforms
Zig

Client Certificate in REST

See more REST Examples

Demonstrates how to use a client certificate with a REST connection.

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
    // (i.e. sets the client-side certificate for two-way TLS authentication)
    // Note: There are other ways to set the client certificate using Chilkat.
    // For example, on Windows systems, a Chilkat certificate object could be loaded with certificate
    // pre-installed (with private key) in a Windows certificate store, and then socket.SetSslClientCert could be called.
    socket.setSslClientCertPfx("/home/bob/pfxFiles/myClientSideCertWithPrivateKey.pfx", "pfxPassword") catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    // Note: The certificate used for the client-side of TLS mutual authentication
    // must have the associated private key available. (.pfx/.p12 files typically store both
    // the certificate and associated private key.)

    // 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;
    };
}