Sample code for 30+ languages & platforms
Xbase++

SMSAPI - Create a Contact

See more SMSAPI Examples

Create a Contact

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oReq
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL oDate_created
LOCAL oDate_updated
LOCAL cName
LOCAL cCreated_by
LOCAL cIdx
LOCAL cContact_expire_after
LOCAL cContacts_count
LOCAL cId
LOCAL cFirst_name
LOCAL cLast_name
LOCAL cPhone_number
LOCAL cEmail
LOCAL cGender
LOCAL cCity
LOCAL cDescription
LOCAL i
LOCAL nCount_i

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oHttp := CreateObject("Chilkat.Http")

//  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

oReq := CreateObject("Chilkat.HttpRequest")
oReq:HttpVerb := "POST"
oReq:Path := "/contacts"
oReq:ContentType := "application/x-www-form-urlencoded"
oReq:AddParam("phone_number", "48500000000")
oReq:AddParam("email", "bok@smsapi.com")
oReq:AddParam("first_name", "Name")
oReq:AddParam("last_name", "Last_name")
oReq:AddParam("gender", "gender")
oReq:AddParam("description", "description")
oReq:AddParam("city", "city")
oReq:AddParam("groups", "default")

oReq:AddHeader("Authorization", "Bearer token_api_oauth")

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpReq("https://api.smsapi.com/contacts", oReq, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    RETURN
ENDIF

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0

? "Response Body:"
? oJResp:Emit()

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
    ? "Response Header:"
    ? oResp:Header
    ? "Failed."
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

//  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

oDate_created := CreateObject("Chilkat.DtObj")
oDate_updated := CreateObject("Chilkat.DtObj")

cId := oJResp:StringOf("id")
cFirst_name := oJResp:StringOf("first_name")
cLast_name := oJResp:StringOf("last_name")
cPhone_number := oJResp:StringOf("phone_number")
cEmail := oJResp:StringOf("email")
cGender := oJResp:StringOf("gender")
cCity := oJResp:StringOf("city")
oJResp:DtOf("date_created", 0, oDate_created)
oJResp:DtOf("date_updated", 0, oDate_updated)
cDescription := oJResp:StringOf("description")
i := 0
nCount_i := oJResp:SizeOfArray("groups")
DO WHILE i < nCount_i
    oJResp:I := i
    cId := oJResp:StringOf("groups[i].id")
    cName := oJResp:StringOf("groups[i].name")
    oJResp:DtOf("groups[i].date_created", 0, oDate_created)
    oJResp:DtOf("groups[i].date_updated", 0, oDate_updated)
    cDescription := oJResp:StringOf("groups[i].description")
    cCreated_by := oJResp:StringOf("groups[i].created_by")
    cIdx := oJResp:StringOf("groups[i].idx")
    cContact_expire_after := oJResp:StringOf("groups[i].contact_expire_after")
    cContacts_count := oJResp:StringOf("groups[i].contacts_count")
    i := i + 1
ENDDO

oHttp:destroy()
oReq:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
oDate_created:destroy()
oDate_updated:destroy()