Sample code for 30+ languages & platforms
Zig

Enumerate REST Response Header Names

See more REST Examples

Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given 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();

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

    rest.sendReqNoBody("GET", "/api/items/123") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const status_code = rest.readResponseHeader();
    if (status_code < 0) {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    }

    // Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
    // and ResponseHdrValue returns the value at the same index.
    const num_headers = rest.getNumResponseHeaders();
    var i: i32 = 0;
    i = 0;
    while (i < num_headers) : (i += 1) {
        const name = rest.responseHdrName(alloc, i) catch {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
            return;
        };

        const value = rest.responseHdrValue(alloc, i) catch {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
            return;
        };

        std.debug.print("{s}: {s}\n", .{ name, value });
    }
}