Sample code for 30+ languages & platforms
Zig

MyInvois Malaysia Get Document Types

See more Malaysia MyInvois Examples

There are multiple types of documents supported by MyInvois, and this API retrieves their definitions through API call.

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

    // Adds the "Authorization: Bearer <access_token>" header.
    http.setAuthToken("<access_token>");

    // Note: The access token is valid for a short amount of time.  Perhaps 1 hour.
    // The access token is used in the "Authorization: Bearer <access_token>" header in subsequent requests until it expires.
    // Your application would then need to get a new access token, and so on..
    // To get an access token, see How to Get a MyInvois Access Token

    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    http.quickGetSb("https://preprod-api.myinvois.hasil.gov.my/api/v1.0/documenttypes", sb) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const status_code = http.getLastStatus();
    std.debug.print("response status code = {d}\n", .{status_code});

    if (status_code != 200) {
        // Failed.
        std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
        return;
    }

    // Sample response:

    // {"result":
    //       [
    //          {"id":45,
    //             "invoiceTypeCode":4,
    //             "description":"Invoice",
    //             "activeFrom":"2015-02-13T13:15:00Z",
    //             "activeTo":"2027-03-01T00:00:00Z",
    //             "documentTypeVersions":
    //             [
    //                {"id":454,
    //                 "name":"1.0",
    //                 "description":"Invoice version 1.1",
    //                 "activeFrom":"2015-02-13T13:15:00Z",
    //                 "activeTo":"2027-03-01T00:00:00Z",
    //                 "versionNumber":1.1,
    //                 "status":"published"
    //                }
    //             ]
    //           }
    //       ]
    // }

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

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

    var id: i32 = 0;
    var invoice_type_code: i32 = 0;
    var description: [:0]const u8 = "";
    var active_from: [:0]const u8 = "";
    var active_to: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var name: [:0]const u8 = "";
    var version_number: [:0]const u8 = "";
    var status: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = json.sizeOfArray("result");
    while (i < count_i) {
        json.setI(i);
        id = json.intOf("result[i].id");
        invoice_type_code = json.intOf("result[i].invoiceTypeCode");
        description = try json.stringOf(alloc, "result[i].description");
        active_from = try json.stringOf(alloc, "result[i].activeFrom");
        active_to = try json.stringOf(alloc, "result[i].activeTo");
        j = 0;
        count_j = json.sizeOfArray("result[i].documentTypeVersions");
        while (j < count_j) {
            json.setJ(j);
            id = json.intOf("result[i].documentTypeVersions[j].id");
            name = try json.stringOf(alloc, "result[i].documentTypeVersions[j].name");
            description = try json.stringOf(alloc, "result[i].documentTypeVersions[j].description");
            active_from = try json.stringOf(alloc, "result[i].documentTypeVersions[j].activeFrom");
            active_to = try json.stringOf(alloc, "result[i].documentTypeVersions[j].activeTo");
            version_number = try json.stringOf(alloc, "result[i].documentTypeVersions[j].versionNumber");
            status = try json.stringOf(alloc, "result[i].documentTypeVersions[j].status");
            j = j + 1;
        }

        i = i + 1;
    }
}