Zig
Zig
Constant Contact - Add Contacts to List
See more Constant Contact Examples
Demonstrates a call to add five existing contacts to two lists.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 -X POST \
// https://api.cc.email/v3/activities/add_list_memberships \
// -H 'Accept: application/json' \
// -H 'Authorization: Bearer {access_token}' \
// -H 'cache-control: no-cache' \
// -H 'content-type: application/json' \
// -d '{
// "source": {
// "contact_ids": [
// "{contact_id1}",
// "{contact_id2}",
// "{contact_id3}",
// "{contact_id4}",
// "{contact_id5}",
// "{contact_id6}"
// ]
// },
// "list_ids": [
// "{list_id1}",
// "{list_id2}"
// ]
// }'
// 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.
// {
// "source": {
// "contact_ids": [
// "{contact_id1}",
// "{contact_id2}",
// "{contact_id3}",
// "{contact_id4}",
// "{contact_id5}",
// "{contact_id6}"
// ]
// },
// "list_ids": [
// "{list_id1}",
// "{list_id2}"
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("source.contact_ids[0]", "{contact_id1}") catch {};
json.updateString("source.contact_ids[1]", "{contact_id2}") catch {};
json.updateString("source.contact_ids[2]", "{contact_id3}") catch {};
json.updateString("source.contact_ids[3]", "{contact_id4}") catch {};
json.updateString("source.contact_ids[4]", "{contact_id5}") catch {};
json.updateString("source.contact_ids[5]", "{contact_id6}") catch {};
json.updateString("list_ids[0]", "{list_id1}") catch {};
json.updateString("list_ids[1]", "{list_id2}") catch {};
// Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.setAuthToken("ACCESS_TOKEN");
http.setRequestHeader("content-type", "application/json");
http.setRequestHeader("Accept", "application/json");
http.setRequestHeader("cache-control", "no-cache");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://api.cc.email/v3/activities/add_list_memberships", 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:
// (Sample code for parsing the JSON response is shown below)
// {
// "activity_id": "activity_id",
// "state": "initialized",
// "created_at": "2018-02-28T13:49:41-05:00",
// "updated_at": "2018-02-28T13:49:41-05:00",
// "percent_done": 1,
// "activity_errors": [
// ],
// "status": {
// "list_count": 2
// },
// "_links": {
// "self": {
// "href": "/v3/activities/activity_id"
// }
// }
// }
// 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, "activity_id");
_ = try j_resp.stringOf(alloc, "state");
_ = try j_resp.stringOf(alloc, "created_at");
_ = try j_resp.stringOf(alloc, "updated_at");
_ = j_resp.intOf("percent_done");
_ = j_resp.intOf("status.list_count");
_ = try j_resp.stringOf(alloc, "_links.self.href");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("activity_errors");
while (i < count_i) {
j_resp.setI(i);
_ = try j_resp.stringOf(alloc, "activity_errors[i]");
i = i + 1;
}
}