Sample code for 30+ languages & platforms
Zig

Use an Existing Socket Connection for REST

See more REST Examples

Demonstrates Rest.UseConnection, which supplies an already-connected Socket as the transport for REST requests instead of calling Connect. This is useful when the connection must be established with specific socket options or shared across objects.

Background. Rest can operate over any connected socket. The second argument enables automatic reconnection so a dropped connection is transparently re-established between requests.

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();

    // Demonstrates supplying an already-connected socket for the REST transport.  This is useful when
    // the connection must be established with specific socket options, or reused across objects.

    const socket = try chilkat.Socket.init();
    defer socket.deinit();
    const b_ssl = true;
    socket.connect("example.com", 443, b_ssl, 5000) catch {
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Use the connected socket as the transport.  The 2nd argument enables automatic reconnection.
    const b_auto_reconnect = true;
    rest.useConnection(socket, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const response_text = rest.fullRequestNoBody(alloc, "GET", "/api/status") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{response_text});
}