Sample code for 30+ languages & platforms
Zig

List all Labels in the User's Mailbox

See more GMail REST API Examples

List all Labels in the GMail User's Mailbox

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();
    http.setAuthToken("GMAIL-ACCESS-TOKEN");

    const user_id = "me";
    http.setUrlVar("userId", user_id) catch {};

    const url = "https://www.googleapis.com/gmail/v1/users/{$userId}/labels";

    http.setSessionLogFilename("c:/temp/qa_output/sessionLog.txt");

    // Get the list of GMail labels as JSON.
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    http.quickGetSb(url, sb) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    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)});

    if (http.getLastStatus() != 200) {
        std.debug.print("Failed.\n", .{});
        return;
    }
}