Zig
Zig
HTTP HEAD Request
See more HTTP Examples
Sends an HTTP HEAD request and gets the response.Note: The response to an HTTP HEAD request is always just the response header. The reponse body is always 0 length (thus the reason it's called a "HEAD" request..)
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// If the URL uses "https://", then the connection will be TLS.
// Otherwise it will be TCP.
// A failure is when we don't get any response. It could be a timeout, an inability to connect, etc.
// For example, a "404 Not Found" response is still a response, and thus deemed success in terms of the API..
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpNoBody("HEAD", "https://example-code.com/", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// Examine the response.
std.debug.print("Status Code = {d}\n", .{resp.getStatusCode()});
std.debug.print("Status Line = {s}\n", .{try resp.getStatusLine(alloc)});
std.debug.print("Status Text = {s}\n", .{try resp.getStatusText(alloc)});
std.debug.print("Full Response Header:\n", .{});
std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
std.debug.print("----\n", .{});
const num_header_fields = resp.getNumHeaderFields();
std.debug.print("Num Header Fields: {d}\n", .{num_header_fields});
var i: i32 = 0;
i = 0;
while (i < num_header_fields) : (i += 1) {
std.debug.print("{s}: {s}\n", .{ try resp.getHeaderName(alloc, i), try resp.getHeaderValue(alloc, i) });
}
}