Sample code for 30+ languages & platforms
Lua

REST POST JSON using Gzip Content Encoding

See more REST Examples

Demonstrates how to send a JSON POST using the gzip Content-Encoding.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

-- Use the online tool at https://tools.chilkat.io/Default.cshtml
-- to generate the JSON code that creates this JSON:

-- {
--     "lists": [
--         {
--             "id": "1511199999"
--         }
--     ],
--     "confirmed": false,
--     "email_addresses": [
--         {
--             "email_address": "support@chilkatsoft.com"
--         }
--     ],
--     "first_name": "Matt",
--     "last_name": "Smith"
-- }
--

local json = chilkat.JsonObject.new()
json:update_string("lists[0].id", "1511199999")
json:update_bool("confirmed", false)
json:update_string("email_addresses[0].email_address", "support@chilkatsoft.com")
json:update_string("first_name", "Matt")
json:update_string("last_name", "Smith")

local rest = chilkat.Rest.new()

local success = rest:connect("api.constantcontact.com", 443, true, true)
if not success then
    print(rest.last_error_text)
    return
end

rest:add_header("Content-Type", "application/json")
rest:add_header("Authorization", "Bearer xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx")

-- Tell the server you'll accept only an application/json response.
rest:add_header("Accept", "application/json")

-- Indicate that we'll be sending the request body gzipped.
rest:add_header("Content-Encoding", "gzip")

local sbReq = chilkat.StringBuilder.new()
json:emit_sb(sbReq)

-- Send the POST.
local sbResp = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/v2/contacts?action_by=ACTION_BY_VISITOR&api_key=xxxxxxx", sbReq, sbResp)
if not success then
    print(rest.last_error_text)
    return
end

print("Response body:")
print(sbResp:get_as_string())

if rest.response_status_code ~= 200 then
    print("Received error response code: " .. rest.response_status_code)
    return
end

local jsonResp = chilkat.JsonObject.new()
jsonResp:load_sb(sbResp)

-- Use the online tool at https://tools.chilkat.io/jsonParse.cshtml
-- to generate the JSON code that parses the JSON response..