Zig
Zig
Get a REST Response Header Value by Index
See more REST Examples
Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.
Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.
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;
};
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;
}
// Return the value of the response header at a zero-based index. Use the same index with
// ResponseHdrName to get the corresponding field name.
const num_headers = rest.getNumResponseHeaders();
var i: i32 = 0;
i = 0;
while (i < num_headers) : (i += 1) {
const value = rest.responseHdrValue(alloc, i) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
std.debug.print("Header {d} value: {s}\n", .{ i, value });
}
}