Sample code for 30+ languages & platforms
Zig

SMSAPI - Edit a Subuser Account

See more SMSAPI Examples

Modify a sub-user account.

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 http = try chilkat.Http.init();
    defer http.deinit();

    // Implements the following CURL command:

    // curl -X PUT -H "Content-Type: application/json" \
    // -H "Authorization: Bearer token_api_oauth" -d\
    // '{"credentials":{"password":"Customer_portal_password","api_password":"API_password"},
    // "active":"1","description":"Test description","points":{"from_account":"2","per_month":"2"}}'
    // 'https://api.smsapi.com/subusers/{subuser_id}'

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

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

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

    // {
    //   "credentials": {
    //     "password": "Customer_portal_password",
    //     "api_password": "API_password"
    //   },
    //   "active": "1",
    //   "description": "Test description",
    //   "points": {
    //     "from_account": "2",
    //     "per_month": "2"
    //   }
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("credentials.password", "new_Password_1234") catch {};
    json.updateString("credentials.api_password", "new_API_password_1234") catch {};
    json.updateString("active", "1") catch {};
    json.updateString("description", "Test description") catch {};
    json.updateString("points.from_account", "3") catch {};
    json.updateString("points.per_month", "3") catch {};

    // Adds the "Authorization: Bearer token_api_oauth" header.
    http.setAuthToken("token_api_oauth");
    http.setRequestHeader("Content-Type", "application/json");

    const sb_request_body = try chilkat.StringBuilder.init();
    defer sb_request_body.deinit();
    json.emitSb(sb_request_body) catch {};

    // In this example, the subuser_id is 5F8BB03E6436384B0AB36F1B
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSb("PUT", "https://api.smsapi.com/subusers/5F8BB03E6436384B0AB36F1B", sb_request_body, "utf-8", "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};
    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

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

    // {
    //   "id": "5F8BB03E6436384B0AB36F1B",
    //   "username": "TestUser",
    //   "active": true,
    //   "description": "Test description",
    //   "points": {
    //     "from_account": 3.0,
    //     "per_month": 3.0
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    _ = try j_resp.stringOf(alloc, "id");
    _ = try j_resp.stringOf(alloc, "username");
    _ = j_resp.boolOf("active");
    _ = try j_resp.stringOf(alloc, "description");
    _ = try j_resp.stringOf(alloc, "points.from_account");
    _ = try j_resp.stringOf(alloc, "points.per_month");
}