Lua
Lua
Constant Contact - Add Contacts to List
See more Constant Contact Examples
Demonstrates a call to add five existing contacts to two lists.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- 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}"
-- ]
-- }
local json = chilkat.JsonObject.new()
json:update_string("source.contact_ids[0]", "{contact_id1}")
json:update_string("source.contact_ids[1]", "{contact_id2}")
json:update_string("source.contact_ids[2]", "{contact_id3}")
json:update_string("source.contact_ids[3]", "{contact_id4}")
json:update_string("source.contact_ids[4]", "{contact_id5}")
json:update_string("source.contact_ids[5]", "{contact_id6}")
json:update_string("list_ids[0]", "{list_id1}")
json:update_string("list_ids[1]", "{list_id2}")
-- Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.auth_token = "ACCESS_TOKEN"
http:set_request_header("content-type", "application/json")
http:set_request_header("Accept", "application/json")
http:set_request_header("cache-control", "no-cache")
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://api.cc.email/v3/activities/add_list_memberships", json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- 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
local activity_id = jResp:string_of("activity_id")
local state = jResp:string_of("state")
local created_at = jResp:string_of("created_at")
local updated_at = jResp:string_of("updated_at")
local percent_done = jResp:int_of("percent_done")
local statusList_count = jResp:int_of("status.list_count")
local v_linksSelfHref = jResp:string_of("_links.self.href")
local i = 0
local count_i = jResp:size_of_array("activity_errors")
while i < count_i do
jResp.i = i
local errMsg = jResp:string_of("activity_errors[i]")
i = i + 1
end