Sample code for 30+ languages & platforms
Zig

HTTPS GET Request with Authorization Bearer Header

See more HTTP Examples

Demonstrates how to send an HTTPS GET request with an "Authorization: Bearer " header and get the text response (which could be JSON).

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 added to each request.
    // Set AuthToken equal to your token without the keyword "Bearer".
    http.setAuthToken("Your access token");

    // Send the GET request and get the response text (assuming you know beforehand that the response will be text, such as JSON, XML, HTML, etc.)
    const response_text = http.quickGetStr(alloc, "https://www.example.com/something") catch {
        // This means something failed where you did not get an HTTP response.
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // If you get here, then you received an HTTP response where you can check the response status code, and the responseText will contain
    // the response body.
    std.debug.print("Response status code = {d}\n", .{http.getLastStatus()});
    if (http.getLastStatus() != 200) {
        // Probably an error from the server.  Examine the HTTP response body.
        std.debug.print("{s}\n", .{response_text});
        return;
    }

    // If you get here, the response status code was the expected value indicating success,
    // if you know the response is JSON, you can load it into a Chilkat JSON object and continue ...
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.setEmitCompact(true);
    json.load(response_text) catch {};
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // ...
    // ...
}