Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

QuickBooks - Full Update an Account

See more QuickBooks Examples

Demonstrates how to update any of the writable fields of an existing account. The example first reads the JSON data for the account, makes some changes, and then POST's the updated JSON to update the 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.

    // Get our previously obtained OAuth2 access token, which should contain JSON like this:
    // {
    //   "expires_in": 3600,
    //   "x_refresh_token_expires_in": 8726400,
    //   "refresh_token": "L011546037639r ... 3vR2DrbOmg0Sdagw",
    //   "access_token": "eyJlbmMiOiJBMTI4Q0 ... oETJEMbeggg",
    //   "token_type": "bearer"
    // }

    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    try json_token.loadFile("qa_data/tokens/qb-access-token.json");

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

    // Connect using TLS.
    // A single REST object, once connected, can be used for many Quickbooks 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("sandbox-quickbooks.api.intuit.com", 443, true, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const sb_auth = try chilkat.StringBuilder.init();
    defer sb_auth.deinit();
    sb_auth.append("Bearer ") catch {};
    sb_auth.append(try json_token.stringOf(alloc, "access_token")) catch {};
    rest.setAuthorization(try sb_auth.getAsString(alloc));

    rest.addHeader("Accept", "application/json") catch {};
    rest.setAllowHeaderFolding(false);

    // First get the full JSON for the account we'll be updating..
    // The company ID is the 123146096291789
    // The account ID is 29
    var response_body: [:0]const u8 = rest.fullRequestNoBody(alloc, "GET", "/v3/company/123146096291789/account/29?minorversion=45") 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("{s}\n", .{response_body});
        return;
    }

    // Load the JSON response into a JSON object for parsing/updating
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(response_body) catch {};

    // Show the JSON..
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});
    std.debug.print("----\n", .{});

    // The JSON passed in the HTTP POST for updating is the inner content of the Account
    const update_json = try chilkat.JsonObject.init();
    defer update_json.deinit();
    json.objectOf2("Account", update_json) catch {};

    // Add or update the Description.
    // (If a Description already exists, it is replace, otherwise the Description is added.)
    update_json.updateString("Description", "Description added during update.") catch {};

    // Examine the JSON we'll be sending in the body of the POST.
    update_json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try update_json.emit(alloc)});
    std.debug.print("----\n", .{});

    // Now we're going to POST the changes to update the Quickbooks account.
    rest.clearAllHeaders() catch {};
    rest.setAuthorization(try sb_auth.getAsString(alloc));
    rest.addHeader("Accept", "application/json") catch {};
    rest.addHeader("Content-Type", "application/json") catch {};

    // The updated JSON is passed as the HTTP request body.
    response_body = rest.fullRequestString(alloc, "POST", "/v3/company/123146096291789/account?minorversion=45", try update_json.emit(alloc)) 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("{s}\n", .{response_body});
        return;
    }

    // Load the JSON response into a JSON object to emit in indented form..
    json.load(response_body) catch {};
    std.debug.print("{s}\n", .{try json.emit(alloc)});

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

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