Sample code for 30+ languages & platforms
Lua

effectconnect Create or Replace Product Catalog

See more effectconnect Examples

Use this call to create or replace a product catalog in EffectConnect. This is always a purge and replace action for the entire catalog.

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 uri = "/products"
local apiVersion = "2.0"

local http = chilkat.Http.new()
local req = chilkat.HttpRequest.new()

-- Use your effectconnect public key here...
req:add_header("KEY", "PUBLIC_KEY")
req:add_header("VERSION", apiVersion)
req:add_header("URI", uri)
req:add_header("RESPONSETYPE", "XML")
req:add_header("RESPONSELANGUAGE", "en")

-- Get the current date/time in timestamp format.
local dt = chilkat.DateTime.new()
dt:set_from_current_system_time()
local timestamp = dt:get_as_timestamp(true)

req:add_header("TIME", timestamp)
print("timestamp = " .. timestamp)

local sbXml = chilkat.StringBuilder.new()
local success = sbXml:load_file("qa_data/xml/effectconnect/effconCreate.xml", "utf-8")
print("length = " .. sbXml.length)

req.http_verb = "POST"
req.path = uri
req.content_type = "multipart/form-data"
success = req:add_string_for_upload("payload", "effcon.xml", sbXml:get_as_string(), "utf-8")
if not success then
    print(req.last_error_text)
    return
end

-- Build a string-to-sign and sign it using our effectconnect private key
local sbStringToSign = chilkat.StringBuilder.new()
sbStringToSign:append_int(sbXml.length)
sbStringToSign:append("POST")
sbStringToSign:append(uri)
sbStringToSign:append(apiVersion)
sbStringToSign:append(timestamp)

local crypt = chilkat.Crypt2.new()
crypt.mac_algorithm = "hmac"
crypt.hash_algorithm = "sha512"
crypt.encoding_mode = "base64"
-- Use your effectconnect private key here:
crypt:set_mac_key_string("PRIVATE_KEY")
req:add_header("SIGNATURE", crypt:mac_string_enc(sbStringToSign:get_as_string()))

local resp = chilkat.HttpResponse.new()
success = http:http_sreq("submit.effectconnect.com", 443, true, req, resp)
if not success then
    print(http.last_error_text)
    return
end

print("response status code = " .. resp.status_code)

-- Examine the response.  The response status code can be 200 for both errors and success.
-- The success or error is based on the XML returned in the response body.
local xmlResp = chilkat.Xml.new()
xmlResp:load_xml(resp.body_str)

print("response body:")
print(xmlResp:get_xml())

-- A sample response:

-- <?xml version="1.0" encoding="utf-8"?>
-- <ApiResponseContainer>
--     <Request>
--         <RequestType>Products</RequestType>
--         <RequestAction>Create</RequestAction>
--         <RequestVersion>2.0</RequestVersion>
--         <RequestIdentifier/>
--         <ProcessedAt>2019-04-18T15:28:55+02:00</ProcessedAt>
--     </Request>
--     <Response>
--         <Result>Success</Result>
--         <ProductsCreateResponseContainer>
--             <ProcessID><![CDATA[J048hgS4OkNn0JnH]]></ProcessID>
--         </ProductsCreateResponseContainer>
--     </Response>
-- </ApiResponseContainer>

-- Parsing the response...
local tagPath = ""

local RequestType = xmlResp:get_child_content("Request|RequestType")
local RequestAction = xmlResp:get_child_content("Request|RequestAction")
local RequestVersion = xmlResp:get_child_content("Request|RequestVersion")
local ProcessedAt = xmlResp:get_child_content("Request|ProcessedAt")
local Result = xmlResp:get_child_content("Response|Result")
local ProcessID = xmlResp:get_child_content("Response|ProductsCreateResponseContainer|ProcessID")