Sample code for 30+ languages & platforms
Zig

HTTP GET with Custom Header and OAuth2 Bearer Token

See more HTTP Examples

Demonstrate how to send a GET request with customer headers and an "Authorization: Bearer " header.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Setting the AuthToken property causes the "Authorization: Bearer <token>" header to be adeded.
    http.setAuthToken("Just_the_access_token_here");

    // Add one or more custom headers..
    http.setRequestHeader("X-Tenant-ID", "value goes here");
    http.setRequestHeader("blah-blah-blah", "value goes here");

    const url = "https://www.example.com/abc/123?x=something&y=someOtherThing";

    // Send the GET request and get the response body in the StringBuilder object.
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    http.quickGetSb(url, sb) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("response status code: {d}\n", .{http.getLastStatus()});
    std.debug.print("response body:\n", .{});
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    // If the response contains JSON, you can load it into a Chilkat JSON object...
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadSb(sb) catch {};
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});
}