Sample code for 30+ languages & platforms
Zig

Get Populi Access Key (Token)

See more Populi Examples

Demonstrates how to authenticate and obtain an access key using a username and password.

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

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Connect using TLS.
    // A single REST object, once connected, can be used for many Populi REST API calls.
    // The auto-reconnect indicates that if the already-established HTTPS connection is closed,
    // then it will be automatically re-established as needed.
    const b_auto_reconnect = true;
    rest.connect("yourcollege.populi.co", 443, true, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    rest.addQueryParam("username", "POPULI_USERNAME") catch {};
    rest.addQueryParam("password", "POPULI_PASSWORD") catch {};

    const response_body = rest.fullRequestFormUrlEncoded(alloc, "POST", "/api/index.php") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // We should expect a 200 response if successful.
    if (rest.getResponseStatusCode() != 200) {
        std.debug.print("Request Header: \n", .{});
        std.debug.print("{s}\n", .{try rest.getLastRequestHeader(alloc)});
        std.debug.print("----\n", .{});
        std.debug.print("Response StatusCode = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("Response StatusLine: {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("Response Body:\n", .{});
        std.debug.print("{s}\n", .{response_body});
        return;
    }

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(response_body) catch {};
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Sample response:
    // <?xml version="1.0" encoding="ISO-8859-1"?>
    // <response>
    // <access_key>63481e36023b5d ... 76d0dbc314</access_key>
    //     <accountid>19999999</accountid>
    //     <accounttype>PERSON</accounttype>
    // </response>

    // Save the XML to a file for subsequent requests..
    xml.saveXml("qa_data/tokens/populi_token.xml") catch {};
}