Sample code for 30+ languages & platforms
Lua

Constant Contact - Create a List

See more Constant Contact Examples

Create a new list using a POST call to the /contact_lists endpoint.

Chilkat Lua Downloads

Lua
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/contact_lists \
--   -H 'Accept: application/json' \
--   -H 'Authorization: Bearer {access_token}' \
--   -H 'cache-control: no-cache' \
--   -H 'content-type: application/json' \
--   -d '{
--   "name": "Multiple purchases",
--   "favorite": true,
--   "description": "List of repeat customers"
-- }'

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

-- {
--   "name": "Multiple purchases",
--   "favorite": true,
--   "description": "List of repeat customers"
-- }

local json = chilkat.JsonObject.new()
json:update_string("name", "Multiple purchases")
json:update_bool("favorite", true)
json:update_string("description", "List of repeat customers")

-- 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/contact_lists", 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)

-- {
--   "list_id": "{list_id}",
--   "name": "Multiple purchases",
--   "description": "List of repeat customers",
--   "favorite": true,
--   "created_at": "2017-07-14T11:25:00-04:00",
--   "updated_at": "2017-07-14T11:25:00-04: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

local list_id = jResp:string_of("list_id")
local name = jResp:string_of("name")
local description = jResp:string_of("description")
local favorite = jResp:bool_of("favorite")
local created_at = jResp:string_of("created_at")
local updated_at = jResp:string_of("updated_at")