Sample code for 30+ languages & platforms
Zig

Amazon Cognito - List Users

See more Amazon Cognito Examples

Lists the users in the Amazon Cognito user pool.

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

    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    auth_aws.setRegion("us-west-2");
    auth_aws.setServiceName("cognito-idp");
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(auth_aws) catch {};

    // URL: https://cognito-idp.us-west-2.amazonaws.com/
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    // Use the same region as specified above.
    rest.connect("cognito-idp.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("ConnectFailReason: {d}\n", .{rest.getConnectFailReason()});
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Note: The above code does not need to be repeatedly called for each REST request.
    // The rest object can be setup once, and then many requests can be sent.  Chilkat will automatically
    // reconnect within a FullRequest* method as needed.  It is only the very first connection that is explicitly
    // made via the Connect method.

    // The following JSON is sent in the request body.

    // {
    //     "UserPoolId": "us-west-2_yt6WzO3SA"
    // }
    //

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("UserPoolId", "us-west-2_yt6WzO3SA") catch {};

    rest.addHeader("Content-Type", "application/x-amz-json-1.0") catch {};
    rest.addHeader("X-Amz-Target", "AWSCognitoIdentityProviderService.ListUsers") catch {};
    rest.addHeader("Accept-Encoding", "identity") catch {};

    const sb_request_body = try chilkat.StringBuilder.init();
    defer sb_request_body.deinit();
    json.emitSb(sb_request_body) catch {};
    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    rest.fullRequestSb("POST", "/", sb_request_body, sb_response_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const resp_status_code = rest.getResponseStatusCode();
    std.debug.print("response status code = {d}\n", .{resp_status_code});
    if (resp_status_code != 200) {
        std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
        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", .{try sb_response_body.getAsString(alloc)});
        return;
    }

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.loadSb(sb_response_body) catch {};

    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // {
    //   "Users": [
    //     {
    //       "Attributes": [
    //         {
    //           "Name": "sub",
    //           "Value": "1874cf28-00d3-4ac1-a24a-68dcb0cecf09"
    //         },
    //         {
    //           "Name": "email_verified",
    //           "Value": "true"
    //         },
    //         {
    //           "Name": "phone_number_verified",
    //           "Value": "true"
    //         },
    //         {
    //           "Name": "phone_number",
    //           "Value": "+16302581871"
    //         },
    //         {
    //           "Name": "email",
    //           "Value": "admin@chilkatsoft.com"
    //         }
    //       ],
    //       "Enabled": true,
    //       "UserCreateDate": 1.636404386657E9,
    //       "UserLastModifiedDate": 1.636404386657E9,
    //       "UserStatus": "FORCE_CHANGE_PASSWORD",
    //       "Username": "matt"
    //     },
    //     {
    //     . . .
    //     }
    //   ]
    // }

    var enabled: bool = false;
    var user_create_date: [:0]const u8 = "";
    var user_last_modified_date: [:0]const u8 = "";
    var user_status: [:0]const u8 = "";
    var username: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var name: [:0]const u8 = "";
    var value: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = json_response.sizeOfArray("Users");
    while (i < count_i) {
        json_response.setI(i);
        enabled = json_response.boolOf("Users[i].Enabled");
        user_create_date = try json_response.stringOf(alloc, "Users[i].UserCreateDate");
        user_last_modified_date = try json_response.stringOf(alloc, "Users[i].UserLastModifiedDate");
        user_status = try json_response.stringOf(alloc, "Users[i].UserStatus");
        username = try json_response.stringOf(alloc, "Users[i].Username");
        j = 0;
        count_j = json_response.sizeOfArray("Users[i].Attributes");
        while (j < count_j) {
            json_response.setJ(j);
            name = try json_response.stringOf(alloc, "Users[i].Attributes[j].Name");
            value = try json_response.stringOf(alloc, "Users[i].Attributes[j].Value");
            j = j + 1;
        }

        i = i + 1;
    }
}