Lua
Lua
Constant Contact: Create a Contact
See more Constant Contact Examples
Creates a single contact.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/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"
-- }
-- }
local json = chilkat.JsonObject.new()
json:update_string("email_address.address", "danipper@example.com")
json:update_string("email_address.permission_to_send", "implicit")
json:update_string("first_name", "David")
json:update_string("last_name", "Nipper")
json:update_string("job_title", "Musician")
json:update_string("company_name", "Acme Corp.")
json:update_string("create_source", "Account")
json:update_int("birthday_month", 11)
json:update_int("birthday_day", 24)
json:update_string("anniversary", "2006-11-15")
json:update_string("steet_addresses.kind", "home")
json:update_string("steet_addresses.street", "123 Kashmir Valley Road")
json:update_string("steet_addresses.city", "Chicago")
json:update_string("steet_addresses.state", "Illinois")
json:update_string("steet_addresses.country", "United States")
http:set_request_header("content-type", "application/json")
-- Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.auth_token = "ACCESS_TOKEN"
http:set_request_header("cache-control", "no-cache")
http:set_request_header("accept", "application/json")
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://api.cc.email/v3/contacts", 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)
-- {
-- "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
local contact_id = jResp:string_of("contact_id")
local email_addressAddress = jResp:string_of("email_address.address")
local email_addressPermission_to_send = jResp:string_of("email_address.permission_to_send")
local email_addressCreated_at = jResp:string_of("email_address.created_at")
local email_addressUpdated_at = jResp:string_of("email_address.updated_at")
local email_addressOpt_in_source = jResp:string_of("email_address.opt_in_source")
local email_addressOpt_in_date = jResp:string_of("email_address.opt_in_date")
local email_addressConfirm_status = jResp:string_of("email_address.confirm_status")
local first_name = jResp:string_of("first_name")
local last_name = jResp:string_of("last_name")
local job_title = jResp:string_of("job_title")
local company_name = jResp:string_of("company_name")
local birthday_month = jResp:int_of("birthday_month")
local birthday_day = jResp:int_of("birthday_day")
local anniversary = jResp:string_of("anniversary")
local create_source = jResp:string_of("create_source")
local created_at = jResp:string_of("created_at")
local updated_at = jResp:string_of("updated_at")