Sample code for 30+ languages & platforms
Dart

Constant Contact - Add Contacts to List

See more Constant Contact Examples

Demonstrates a call to add five existing contacts to two lists.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

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

  final json = CkJsonObject();
  json.updateString('source.contact_ids[0]', '{contact_id1}');
  json.updateString('source.contact_ids[1]', '{contact_id2}');
  json.updateString('source.contact_ids[2]', '{contact_id3}');
  json.updateString('source.contact_ids[3]', '{contact_id4}');
  json.updateString('source.contact_ids[4]', '{contact_id5}');
  json.updateString('source.contact_ids[5]', '{contact_id6}');
  json.updateString('list_ids[0]', '{list_id1}');
  json.updateString('list_ids[1]', '{list_id2}');

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

  final resp = CkHttpResponse();
  try {
    http.httpJson('POST', 'https://api.cc.email/v3/activities/add_list_memberships', json, 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sbResponseBody = CkStringBuilder();
  resp.getBodySb(sbResponseBody);
  final jResp = CkJsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = resp.statusCode;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(resp.header);
    print('Failed.');
    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

  final activityId = jResp.stringOf('activity_id');
  final state = jResp.stringOf('state');
  final createdAt = jResp.stringOf('created_at');
  final updatedAt = jResp.stringOf('updated_at');
  final percentDone = jResp.intOf('percent_done');
  final statusListCount = jResp.intOf('status.list_count');
  final vLinksSelfHref = jResp.stringOf('_links.self.href');
  var i = 0;
  final countI = jResp.sizeOfArray('activity_errors');
  while (i < countI) {
    jResp.i = i;
    final errMsg = jResp.stringOf('activity_errors[i]');
    i++;
  }
}