self_signed_certs/
main.rs

1use hyper_custom_cert::HttpClient;
2use std::collections::HashMap;
3use std::time::Duration;
4
5#[tokio::main]
6async fn main() {
7    // Default secure client (uses OS trust store when built with default features)
8    let mut headers = HashMap::new();
9    headers.insert("x-app".into(), "example".into());
10
11    let client = HttpClient::builder()
12        .with_timeout(Duration::from_secs(10))
13        .with_default_headers(headers)
14        .build();
15
16    // Demonstrate a request (now returns HttpResponse with raw body data)
17    let _response = client
18        .request_with_options("https://example.com", None)
19        .await
20        .expect("request should succeed on native targets");
21
22    // Production with rustls + custom Root CA (e.g., self-signed for your private service)
23    // Note: Requires building with: --no-default-features --features rustls
24    #[cfg(feature = "rustls")]
25    {
26        // Option 1: Load CA certificate from raw PEM bytes
27        let ca_pem: &[u8] =
28            b"-----BEGIN CERTIFICATE-----\n...your root ca...\n-----END CERTIFICATE-----\n";
29        let _rustls_client = HttpClient::builder()
30            .with_timeout(Duration::from_secs(10))
31            .with_root_ca_pem(ca_pem)
32            .build();
33        let _ = _rustls_client
34            .request_with_options("https://private.local", None)
35            .await;
36
37        // Option 2: Load CA certificate from a file path
38        // Note: This will panic if the file doesn't exist - ensure your cert file is available
39        // let _rustls_client_from_file = HttpClient::builder()
40        //     .with_timeout(Duration::from_secs(10))
41        //     .with_root_ca_file("path/to/your/root-ca.pem")
42        //     .build();
43        // let _ = _rustls_client_from_file.request("https://private.local");
44    }
45
46    // Local development only: accept invalid/self-signed certs (dangerous)
47    // Build with: --features insecure-dangerous (or with rustls,insecure-dangerous)
48    #[cfg(feature = "insecure-dangerous")]
49    {
50        // Shortcut:
51        let _dev_client = HttpClient::with_self_signed_certs();
52        let _ = _dev_client
53            .request_with_options("https://localhost:8443", None)
54            .await;
55
56        // Or explicit builder method:
57        let _dev_client2 = HttpClient::builder()
58            .insecure_accept_invalid_certs(true)
59            .build();
60        let _ = _dev_client2
61            .request_with_options("https://localhost:8443", None)
62            .await;
63    }
64
65    println!("Example finished. See README for feature flags and commands.");
66}