Zig
Zig
Examine HTTP Response Status Code and Text
See more REST Examples
Demonstrates how to examine the HTTP response status code and text when using the REST object.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// We're going to check https://authenticationtest.com/HTTPAuth/
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
rest.connect("authenticationtest.com", port, b_tls, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Send a GET request
_ = rest.fullRequestNoBody(alloc, "GET", "/HTTPAuth/") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// The start line of an HTTP response, called the status line, contains the following information:
//
// The protocol version, usually HTTP/1.1.
// A status code, indicating success or failure of the request. Common status codes are 200, 404, or 302
// A status text. A brief, purely informational, textual description of the status code to help a human understand the HTTP message.
//
// A typical status line looks like: HTTP/1.1 404 Not Found.
// The ResponseStatusCode property contains the integer response code:
std.debug.print("Response status code = {d}\n", .{rest.getResponseStatusCode()});
// The ResponseStatusText property contains the text (if any) that follows the status code on the status line.
std.debug.print("Response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
// In this case, the sample output is:
//
// Response status code = 401
// Response status text = Unauthorized
}