Sample code for 30+ languages & platforms
Zig

GetHarvest - Get Client Contacts

See more GetHarvest Examples

Returns a list of your contacts.

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 "https://api.harvestapp.com/v2/contacts" \
    //   -H "Authorization: Bearer ACCESS_TOKEN" \
    //   -H "Harvest-Account-Id: ACCOUNT_ID" \
    //   -H "User-Agent: MyApp (yourname@example.com)"

    http.setRequestHeader("User-Agent", "MyApp (yourname@example.com)");
    http.setRequestHeader("Authorization", "Bearer ACCESS_TOKEN");
    http.setRequestHeader("Harvest-Account-Id", "ACCOUNT_ID");

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.harvestapp.com/v2/contacts", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    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 = http.getLastStatus();
    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 http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:

    // {
    //   "contacts": [
    //     {
    //       "id": 4706479,
    //       "title": "Owner",
    //       "first_name": "Jane",
    //       "last_name": "Doe",
    //       "email": "janedoe@example.com",
    //       "phone_office": "(203) 697-8885",
    //       "phone_mobile": "(203) 697-8886",
    //       "fax": "(203) 697-8887",
    //       "created_at": "2017-06-26T21:20:07Z",
    //       "updated_at": "2017-06-26T21:27:07Z",
    //       "client": {
    //         "id": 5735774,
    //         "name": "ABC Corp"
    //       }
    //     },
    //     {
    //       "id": 4706453,
    //       "title": "Manager",
    //       "first_name": "Richard",
    //       "last_name": "Roe",
    //       "email": "richardroe@example.com",
    //       "phone_office": "(318) 515-5905",
    //       "phone_mobile": "(318) 515-5906",
    //       "fax": "(318) 515-5907",
    //       "created_at": "2017-06-26T21:06:55Z",
    //       "updated_at": "2017-06-26T21:27:20Z",
    //       "client": {
    //         "id": 5735776,
    //         "name": "123 Industries"
    //       }
    //     }
    //   ],
    //   "per_page": 100,
    //   "total_pages": 1,
    //   "total_entries": 2,
    //   "next_page": null,
    //   "previous_page": null,
    //   "page": 1,
    //   "links": {
    //     "first": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100",
    //     "next": null,
    //     "previous": null,
    //     "last": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100"
    //   }
    // }

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

    var i: i32 = 0;
    var id: i32 = 0;
    var title: [:0]const u8 = "";
    var first_name: [:0]const u8 = "";
    var last_name: [:0]const u8 = "";
    var email: [:0]const u8 = "";
    var phone_office: [:0]const u8 = "";
    var phone_mobile: [:0]const u8 = "";
    var fax: [:0]const u8 = "";
    var created_at: [:0]const u8 = "";
    var updated_at: [:0]const u8 = "";
    var client_id: i32 = 0;
    var client_name: [:0]const u8 = "";

    _ = j_resp.intOf("per_page");
    _ = j_resp.intOf("total_pages");
    _ = j_resp.intOf("total_entries");
    _ = try j_resp.stringOf(alloc, "next_page");
    _ = try j_resp.stringOf(alloc, "previous_page");
    _ = j_resp.intOf("page");
    _ = try j_resp.stringOf(alloc, "links.first");
    _ = try j_resp.stringOf(alloc, "links.next");
    _ = try j_resp.stringOf(alloc, "links.previous");
    _ = try j_resp.stringOf(alloc, "links.last");
    i = 0;
    const count_i = j_resp.sizeOfArray("contacts");
    while (i < count_i) {
        j_resp.setI(i);
        id = j_resp.intOf("contacts[i].id");
        title = try j_resp.stringOf(alloc, "contacts[i].title");
        first_name = try j_resp.stringOf(alloc, "contacts[i].first_name");
        last_name = try j_resp.stringOf(alloc, "contacts[i].last_name");
        email = try j_resp.stringOf(alloc, "contacts[i].email");
        phone_office = try j_resp.stringOf(alloc, "contacts[i].phone_office");
        phone_mobile = try j_resp.stringOf(alloc, "contacts[i].phone_mobile");
        fax = try j_resp.stringOf(alloc, "contacts[i].fax");
        created_at = try j_resp.stringOf(alloc, "contacts[i].created_at");
        updated_at = try j_resp.stringOf(alloc, "contacts[i].updated_at");
        client_id = j_resp.intOf("contacts[i].client.id");
        client_name = try j_resp.stringOf(alloc, "contacts[i].client.name");
        i = i + 1;
    }
}