Sample code for 30+ languages & platforms
Rust

Client Certificate in REST

See more REST Examples

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

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
// (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.
if socket.set_ssl_client_cert_pfx("/home/bob/pfxFiles/myClientSideCertWithPrivateKey.pfx", "pfxPassword").is_err() {
    println!("{}", socket.last_error_text());
    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).
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;
};