Zig
Zig
Get a REST Response Header by Name
See more REST Examples
Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.
Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.
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 a response header field by name. Header field names are case-insensitive.
const content_type = rest.responseHdrByName(alloc, "Content-Type") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
std.debug.print("Content-Type: {s}\n", .{content_type});
}