Sample code for 30+ languages & platforms
AutoIt

SMSAPI - Create a Contact

See more SMSAPI Examples

Create a Contact

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oHttp = ObjCreate("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 = ObjCreate("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 = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpReq("https://api.smsapi.com/contacts",$oReq,$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oResp.GetBodySb($oSbResponseBody)
$oJResp = ObjCreate("Chilkat.JsonObject")
$oJResp.LoadSb($oSbResponseBody)
$oJResp.EmitCompact = False

ConsoleWrite("Response Body:" & @CRLF)
ConsoleWrite($oJResp.Emit() & @CRLF)

Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
    ConsoleWrite("Response Header:" & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
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 = ObjCreate("Chilkat.DtObj")
$oDate_updated = ObjCreate("Chilkat.DtObj")
Local $sName
Local $sCreated_by
Local $sIdx
Local $sContact_expire_after
Local $sContacts_count

Local $sId = $oJResp.StringOf("id")
Local $sFirst_name = $oJResp.StringOf("first_name")
Local $sLast_name = $oJResp.StringOf("last_name")
Local $sPhone_number = $oJResp.StringOf("phone_number")
Local $sEmail = $oJResp.StringOf("email")
Local $sGender = $oJResp.StringOf("gender")
Local $sCity = $oJResp.StringOf("city")
$oJResp.DtOf("date_created",False,$oDate_created)
$oJResp.DtOf("date_updated",False,$oDate_updated)
Local $sDescription = $oJResp.StringOf("description")
Local $i = 0
Local $iCount_i = $oJResp.SizeOfArray("groups")
While $i < $iCount_i
    $oJResp.I = $i
    $sId = $oJResp.StringOf("groups[i].id")
    $sName = $oJResp.StringOf("groups[i].name")
    $oJResp.DtOf("groups[i].date_created",False,$oDate_created)
    $oJResp.DtOf("groups[i].date_updated",False,$oDate_updated)
    $sDescription = $oJResp.StringOf("groups[i].description")
    $sCreated_by = $oJResp.StringOf("groups[i].created_by")
    $sIdx = $oJResp.StringOf("groups[i].idx")
    $sContact_expire_after = $oJResp.StringOf("groups[i].contact_expire_after")
    $sContacts_count = $oJResp.StringOf("groups[i].contacts_count")
    $i = $i + 1
Wend