Sample code for 30+ languages & platforms
Rust

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

Rust

// 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.
let rest = chilkat::Rest::new();
let socket = chilkat::Socket::new();

// Set the certificate to be used for mutual TLS authentication
let cert = chilkat::Cert::new();

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

if cert.load_from_smartcard("").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

if socket.set_ssl_client_cert(&cert).is_err() {
    println!("{}", socket.last_error_text());
    return;
}

// Establish the connection using the socket object (with client certificate authentication).
let b_tls = true;
let port = 443;
let max_wait_ms = 5000;
if socket.connect("www.example.com", port, b_tls, max_wait_ms).is_err() {
    println!("Connect Failure Error Code: {}", socket.connect_fail_reason());
    println!("{}", socket.last_error_text());
    return;
}

let b_auto_reconnect = true;

// Use the connection:
if rest.use_connection(&socket, b_auto_reconnect).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// At this point we are connected and can make REST calls...
// For example..
let Ok(response_json) = rest.full_request_no_body("GET", "/someQuery") else {
    println!("{}", rest.last_error_text());
    return;
};