Rust
Rust
Configure HTTP Basic Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthBasic, which configures HTTP Basic authentication from a username and password. Chilkat automatically adds the Authorization header to each request sent through the object.
Background. HTTP Basic authentication transmits credentials with every request, so it should be used over HTTPS. Credentials should be obtained from a secure source rather than hard-coded in production code.
Chilkat Rust Downloads
let rest = chilkat::Rest::new();
let b_tls = true;
let b_auto_reconnect = true;
if rest.connect("example.com", 443, b_tls, b_auto_reconnect).is_err() {
println!("{}", rest.last_error_text());
return;
}
// Normally you would not hard-code secrets in source. You should instead obtain them from an
// interactive prompt, environment variable, or a secrets vault.
let username = "myUsername".to_string();
let password = "myPassword".to_string();
// Configure HTTP Basic authentication. Chilkat automatically adds the Authorization header to
// each request sent through this object.
if rest.set_auth_basic(&username, &password).is_err() {
println!("{}", rest.last_error_text());
return;
}
let Ok(response_text) = rest.full_request_no_body("GET", "/api/protected") else {
println!("{}", rest.last_error_text());
return;
};
println!("{}", response_text);