Sample code for 30+ languages & platforms
Zig

Constant Contact: Create a Contact

See more Constant Contact Examples

Creates a single contact.

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 -X POST \
    //   https://api.cc.email/v3/contacts \
    //   -H 'accept: application/json' \
    //   -H 'authorization: Bearer {access_token}' \
    //   -H 'cache-control: no-cache' \
    //   -H 'content-type: application/json' \
    //   -d '{
    //   "email_address": {
    //     "address": "danipper@example.com",
    //     "permission_to_send": "implicit"
    //   },
    //   "first_name": "David",
    //   "last_name": "Nipper",
    //   "job_title": "Musician",
    //   "company_name": "Acme Corp.",
    //   "create_source": "Account",
    //   "birthday_month": 11,
    //   "birthday_day": 24,
    //   "anniversary": "2006-11-15",
    //   "steet_addresses":{
    //     "kind":"home",
    //     "street": "123 Kashmir Valley Road",
    //     "city": "Chicago",
    //     "state": "Illinois",
    //     "country": "United States",
    //   }
    // }'

    // 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.

    // {
    //   "email_address": {
    //     "address": "danipper@example.com",
    //     "permission_to_send": "implicit"
    //   },
    //   "first_name": "David",
    //   "last_name": "Nipper",
    //   "job_title": "Musician",
    //   "company_name": "Acme Corp.",
    //   "create_source": "Account",
    //   "birthday_month": 11,
    //   "birthday_day": 24,
    //   "anniversary": "2006-11-15",
    //   "steet_addresses": {
    //     "kind": "home",
    //     "street": "123 Kashmir Valley Road",
    //     "city": "Chicago",
    //     "state": "Illinois",
    //     "country": "United States"
    //   }
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("email_address.address", "danipper@example.com") catch {};
    json.updateString("email_address.permission_to_send", "implicit") catch {};
    json.updateString("first_name", "David") catch {};
    json.updateString("last_name", "Nipper") catch {};
    json.updateString("job_title", "Musician") catch {};
    json.updateString("company_name", "Acme Corp.") catch {};
    json.updateString("create_source", "Account") catch {};
    json.updateInt("birthday_month", 11) catch {};
    json.updateInt("birthday_day", 24) catch {};
    json.updateString("anniversary", "2006-11-15") catch {};
    json.updateString("steet_addresses.kind", "home") catch {};
    json.updateString("steet_addresses.street", "123 Kashmir Valley Road") catch {};
    json.updateString("steet_addresses.city", "Chicago") catch {};
    json.updateString("steet_addresses.state", "Illinois") catch {};
    json.updateString("steet_addresses.country", "United States") catch {};

    http.setRequestHeader("content-type", "application/json");
    // Adds the "Authorization: Bearer ACCESS_TOKEN" header.
    http.setAuthToken("ACCESS_TOKEN");
    http.setRequestHeader("cache-control", "no-cache");
    http.setRequestHeader("accept", "application/json");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://api.cc.email/v3/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:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "contact_id": "{contact_id}",
    //   "email_address": {
    //     "address": "danipper@example.com",
    //     "permission_to_send": "implicit",
    //     "created_at": "2018-02-23T13:38:28-05:00",
    //     "updated_at": "2018-02-23T13:38:28-05:00",
    //     "opt_in_source": "Account",
    //     "opt_in_date": "2018-02-23T13:38:28-05:00",
    //     "confirm_status": "off"
    //   },
    //   "first_name": "David",
    //   "last_name": "Nipper",
    //   "job_title": "Musician",
    //   "company_name": "Acme Corp.",
    //   "birthday_month": 11,
    //   "birthday_day": 24,
    //   "anniversary": "2006-11-15",
    //   "create_source": "Account",
    //   "created_at": "2018-02-23T13:38:28-05:00",
    //   "updated_at": "2018-02-23T13:38:28-05:00"
    // }

    // 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, "contact_id");
    _ = try j_resp.stringOf(alloc, "email_address.address");
    _ = try j_resp.stringOf(alloc, "email_address.permission_to_send");
    _ = try j_resp.stringOf(alloc, "email_address.created_at");
    _ = try j_resp.stringOf(alloc, "email_address.updated_at");
    _ = try j_resp.stringOf(alloc, "email_address.opt_in_source");
    _ = try j_resp.stringOf(alloc, "email_address.opt_in_date");
    _ = try j_resp.stringOf(alloc, "email_address.confirm_status");
    _ = try j_resp.stringOf(alloc, "first_name");
    _ = try j_resp.stringOf(alloc, "last_name");
    _ = try j_resp.stringOf(alloc, "job_title");
    _ = try j_resp.stringOf(alloc, "company_name");
    _ = j_resp.intOf("birthday_month");
    _ = j_resp.intOf("birthday_day");
    _ = try j_resp.stringOf(alloc, "anniversary");
    _ = try j_resp.stringOf(alloc, "create_source");
    _ = try j_resp.stringOf(alloc, "created_at");
    _ = try j_resp.stringOf(alloc, "updated_at");
}