Lua
Lua
Constant Contact - Restore a Deleted Contact
See more Constant Contact Examples
Restores a previously deleted contact with a given id.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 PUT \
-- https://api.cc.email/v3/contacts/{contact_id} \
-- -H 'Accept: application/json' \
-- -H 'Authorization: Bearer {access_token}' \
-- -H 'Cache-Control: no-cache' \
-- -H 'Content-Type: application/json' \
-- -d '{
--
-- "first_name": "John",
-- "last_name": "Byrd",
-- "email_address": {
-- "address": "example@example.com",
-- "permission_to_send": "explicit"
-- },
-- "update_source": "Contact",
-- "list_memberships":["{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.
-- {
-- "first_name": "John",
-- "last_name": "Byrd",
-- "email_address": {
-- "address": "example@example.com",
-- "permission_to_send": "explicit"
-- },
-- "update_source": "Contact",
-- "list_memberships": [
-- "{list_id1}",
-- "{list_id2}"
-- ]
-- }
local json = chilkat.JsonObject.new()
json:update_string("first_name", "John")
json:update_string("last_name", "Byrd")
json:update_string("email_address.address", "example@example.com")
json:update_string("email_address.permission_to_send", "explicit")
json:update_string("update_source", "Contact")
json:update_string("list_memberships[0]", "{list_id1}")
json:update_string("list_memberships[1]", "{list_id2}")
-- Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.auth_token = "ACCESS_TOKEN"
http:set_request_header("Accept", "application/json")
http:set_request_header("Content-Type", "application/json")
http:set_request_header("Cache-Control", "no-cache")
local sbRequestBody = chilkat.StringBuilder.new()
json:emit_sb(sbRequestBody)
local resp = chilkat.HttpResponse.new()
local success = http:http_sb("PUT", "https://api.cc.email/v3/contacts/{contact_id}", sbRequestBody, "utf-8", "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}",
-- "first_name": "John",
-- "last_name": "Byrd",
-- "update_source": "Contact",
-- "create_source": "Account",
-- "created_at": "2013-04-01T15:07:07-04:00",
-- "updated_at": "2018-02-20T16:47:40-05:00",
-- "email_address": {
-- "address": "example@example.com",
-- "permission_to_send": "implicit",
-- "created_at": "2013-04-01T15:07:07-04:00",
-- "updated_at": "2018-02-20T16:47:40-05:00",
-- "opt_in_source": "Account",
-- "opt_in_date": "2015-03-17T11:13:28-04:00",
-- "confirm_status": "off"
-- }
-- }
-- 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 first_name = jResp:string_of("first_name")
local last_name = jResp:string_of("last_name")
local update_source = jResp:string_of("update_source")
local create_source = jResp:string_of("create_source")
local created_at = jResp:string_of("created_at")
local updated_at = jResp:string_of("updated_at")
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")