Zig
Zig
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 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;
};
// 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 username = "myUsername";
const password = "myPassword";
// Configure HTTP Basic authentication. Chilkat automatically adds the Authorization header to
// each request sent through this object.
rest.setAuthBasic(username, 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});
}