Lua
Lua
SMSAPI - Create a Contact
See more SMSAPI Examples
Create a ContactChilkat 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.smsapi.com/contacts -H "Authorization: Bearer token_api_oauth" \
-- -d "phone_number=48500000000&email=bok@smsapi.com&first_name=Name&last_name=Last_name&gender=gender&description=description&city=city&groups=default"
-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/contacts"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("phone_number", "48500000000")
req:add_param("email", "bok@smsapi.com")
req:add_param("first_name", "Name")
req:add_param("last_name", "Last_name")
req:add_param("gender", "gender")
req:add_param("description", "description")
req:add_param("city", "city")
req:add_param("groups", "default")
req:add_header("Authorization", "Bearer token_api_oauth")
local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://api.smsapi.com/contacts", req, 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)
-- {
-- "id": "5b802315a788494a04690d1d",
-- "first_name": "string",
-- "last_name": "string",
-- "phone_number": "48500000000",
-- "email": "bok@smsapi.com",
-- "gender": "gender",
-- "city": "city",
-- "date_created": "2018-08-24T17:24:05+02:00",
-- "date_updated": "2018-08-24T17:24:05+02:00",
-- "description": "description",
-- "groups": [
-- {
-- "id": "59a3ca1fa78849062837cd0c",
-- "name": "default",
-- "date_created": "2017-08-28T09:45:35+02:00",
-- "date_updated": "2017-08-28T09:45:35+02:00",
-- "description": "",
-- "created_by": "username",
-- "idx": null,
-- "contact_expire_after": null,
-- "contacts_count": null
-- }
-- ]
-- }
-- 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 date_created = chilkat.DtObj.new()
local date_updated = chilkat.DtObj.new()
local name = ""
local created_by = ""
local idx = ""
local contact_expire_after = ""
local contacts_count = ""
local id = jResp:string_of("id")
local first_name = jResp:string_of("first_name")
local last_name = jResp:string_of("last_name")
local phone_number = jResp:string_of("phone_number")
local email = jResp:string_of("email")
local gender = jResp:string_of("gender")
local city = jResp:string_of("city")
jResp:dt_of("date_created", false, date_created)
jResp:dt_of("date_updated", false, date_updated)
local description = jResp:string_of("description")
local i = 0
local count_i = jResp:size_of_array("groups")
while i < count_i do
jResp.i = i
id = jResp:string_of("groups[i].id")
name = jResp:string_of("groups[i].name")
jResp:dt_of("groups[i].date_created", false, date_created)
jResp:dt_of("groups[i].date_updated", false, date_updated)
description = jResp:string_of("groups[i].description")
created_by = jResp:string_of("groups[i].created_by")
idx = jResp:string_of("groups[i].idx")
contact_expire_after = jResp:string_of("groups[i].contact_expire_after")
contacts_count = jResp:string_of("groups[i].contacts_count")
i = i + 1
end