Zig
Zig
GetHarvest - Create Contact
See more GetHarvest Examples
Creates a new contact object. Returns a contact object and a 201 Created response code if the call succeeded.Chilkat Zig Downloads
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)" \
// -X POST \
// -H "Content-Type: application/json" \
// -d '{"client_id":8282839,"first_name":"George","last_name":"Frank","email":"georgefrank@example.com"}'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "client_id": 8282839,
// "first_name": "George",
// "last_name": "Frank",
// "email": "georgefrank@example.com"
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateInt("client_id", 8282839) catch {};
json.updateString("first_name", "George") catch {};
json.updateString("last_name", "Frank") catch {};
json.updateString("email", "georgefrank@example.com") catch {};
http.setRequestHeader("User-Agent", "MyApp (yourname@example.com)");
http.setRequestHeader("Content-Type", "application/json");
http.setRequestHeader("Authorization", "Bearer ACCESS_TOKEN");
http.setRequestHeader("Harvest-Account-Id", "ACCOUNT_ID");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://api.harvestapp.com/v2/contacts", json, "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:
// {
// "id": 4706510,
// "title": null,
// "first_name": "George",
// "last_name": "Frank",
// "email": "georgefrank@example.com",
// "phone_office": "",
// "phone_mobile": "",
// "fax": "",
// "created_at": "2017-06-26T21:44:57Z",
// "updated_at": "2017-06-26T21:44:57Z",
// "client": {
// "id": 5735776,
// "name": "123 Industries"
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
_ = j_resp.intOf("id");
_ = try j_resp.stringOf(alloc, "title");
_ = try j_resp.stringOf(alloc, "first_name");
_ = try j_resp.stringOf(alloc, "last_name");
_ = try j_resp.stringOf(alloc, "email");
_ = try j_resp.stringOf(alloc, "phone_office");
_ = try j_resp.stringOf(alloc, "phone_mobile");
_ = try j_resp.stringOf(alloc, "fax");
_ = try j_resp.stringOf(alloc, "created_at");
_ = try j_resp.stringOf(alloc, "updated_at");
_ = j_resp.intOf("client.id");
_ = try j_resp.stringOf(alloc, "client.name");
}