Zig
Zig
Configure HTTP Basic Authentication with SecureString
See more REST Examples
Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.
Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
const rest = try chilkat.Rest.init();
defer rest.deinit();
const b_tls = true;
const b_auto_reconnect = true;
rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
// the credentials encrypted in memory.
// Normally you would not hard-code secrets in source. You should instead obtain them from an
// interactive prompt, environment variable, or a secrets vault.
const ss_username = try chilkat.SecureString.init();
defer ss_username.deinit();
ss_username.append("myUsername") catch {};
const ss_password = try chilkat.SecureString.init();
defer ss_password.deinit();
ss_password.append("myPassword") catch {};
rest.setAuthBasicSecure(ss_username, ss_password) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const response_text = rest.fullRequestNoBody(alloc, "GET", "/api/protected") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{response_text});
}