React Native
React Native
Constant Contact - Add Contacts to List
See more Constant Contact Examples
Demonstrates a call to add five existing contacts to two lists.Chilkat React Native Downloads
import { Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
// 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 = new JsonObject();
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');
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', 'https://api.cc.email/v3/activities/add_list_memberships', json, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
const sbResponseBody = new StringBuilder();
resp.getBodySb(sbResponseBody);
const jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
console.log('Response Body:');
console.log(jResp.emit());
const respStatusCode = resp.statusCode;
console.log(`Response Status Code = ${respStatusCode}`);
if (respStatusCode >= 400) {
console.log('Response Header:');
console.log(resp.header);
console.log('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
const activityId = jResp.stringOf('activity_id');
const state = jResp.stringOf('state');
const createdAt = jResp.stringOf('created_at');
const updatedAt = jResp.stringOf('updated_at');
const percentDone = jResp.intOf('percent_done');
const statusListCount = jResp.intOf('status.list_count');
const vLinksSelfHref = jResp.stringOf('_links.self.href');
let i = 0;
const countI = jResp.sizeOfArray('activity_errors');
while (i < countI) {
jResp.i = i;
const errMsg = jResp.stringOf('activity_errors[i]');
i++;
}
}