Sample code for 30+ languages & platforms
Unicode C++

Constant Contact: Create a Contact

See more Constant Contact Examples

Creates a single contact.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttpW http;

    // 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"
    //   }
    // }

    CkJsonObjectW json;
    json.UpdateString(L"email_address.address",L"danipper@example.com");
    json.UpdateString(L"email_address.permission_to_send",L"implicit");
    json.UpdateString(L"first_name",L"David");
    json.UpdateString(L"last_name",L"Nipper");
    json.UpdateString(L"job_title",L"Musician");
    json.UpdateString(L"company_name",L"Acme Corp.");
    json.UpdateString(L"create_source",L"Account");
    json.UpdateInt(L"birthday_month",11);
    json.UpdateInt(L"birthday_day",24);
    json.UpdateString(L"anniversary",L"2006-11-15");
    json.UpdateString(L"steet_addresses.kind",L"home");
    json.UpdateString(L"steet_addresses.street",L"123 Kashmir Valley Road");
    json.UpdateString(L"steet_addresses.city",L"Chicago");
    json.UpdateString(L"steet_addresses.state",L"Illinois");
    json.UpdateString(L"steet_addresses.country",L"United States");

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

    CkHttpResponseW resp;
    success = http.HttpJson(L"POST",L"https://api.cc.email/v3/contacts",json,L"application/json",resp);
    if (success == false) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    CkStringBuilderW sbResponseBody;
    resp.GetBodySb(sbResponseBody);
    CkJsonObjectW jResp;
    jResp.LoadSb(sbResponseBody);
    jResp.put_EmitCompact(false);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",jResp.emit());

    int respStatusCode = resp.get_StatusCode();
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",resp.header());
        wprintf(L"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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    const wchar_t *contact_id = jResp.stringOf(L"contact_id");
    const wchar_t *email_addressAddress = jResp.stringOf(L"email_address.address");
    const wchar_t *email_addressPermission_to_send = jResp.stringOf(L"email_address.permission_to_send");
    const wchar_t *email_addressCreated_at = jResp.stringOf(L"email_address.created_at");
    const wchar_t *email_addressUpdated_at = jResp.stringOf(L"email_address.updated_at");
    const wchar_t *email_addressOpt_in_source = jResp.stringOf(L"email_address.opt_in_source");
    const wchar_t *email_addressOpt_in_date = jResp.stringOf(L"email_address.opt_in_date");
    const wchar_t *email_addressConfirm_status = jResp.stringOf(L"email_address.confirm_status");
    const wchar_t *first_name = jResp.stringOf(L"first_name");
    const wchar_t *last_name = jResp.stringOf(L"last_name");
    const wchar_t *job_title = jResp.stringOf(L"job_title");
    const wchar_t *company_name = jResp.stringOf(L"company_name");
    int birthday_month = jResp.IntOf(L"birthday_month");
    int birthday_day = jResp.IntOf(L"birthday_day");
    const wchar_t *anniversary = jResp.stringOf(L"anniversary");
    const wchar_t *create_source = jResp.stringOf(L"create_source");
    const wchar_t *created_at = jResp.stringOf(L"created_at");
    const wchar_t *updated_at = jResp.stringOf(L"updated_at");
    }