Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) SMSAPI - Create a ContactCreate a Contact For more information, see https://www.smsapi.com/docs?shell#create-contact
IncludeFile "CkJsonObject.pb" IncludeFile "CkHttp.pb" IncludeFile "CkDtObj.pb" IncludeFile "CkHttpRequest.pb" IncludeFile "CkHttpResponse.pb" IncludeFile "CkStringBuilder.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i ; 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 req.i = CkHttpRequest::ckCreate() If req.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpRequest::setCkHttpVerb(req, "POST") CkHttpRequest::setCkPath(req, "/contacts") CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded") CkHttpRequest::ckAddParam(req,"phone_number","48500000000") CkHttpRequest::ckAddParam(req,"email","bok@smsapi.com") CkHttpRequest::ckAddParam(req,"first_name","Name") CkHttpRequest::ckAddParam(req,"last_name","Last_name") CkHttpRequest::ckAddParam(req,"gender","gender") CkHttpRequest::ckAddParam(req,"description","description") CkHttpRequest::ckAddParam(req,"city","city") CkHttpRequest::ckAddParam(req,"groups","default") CkHttpRequest::ckAddHeader(req,"Authorization","Bearer token_api_oauth") resp.i = CkHttp::ckPostUrlEncoded(http,"https://api.smsapi.com/contacts",req) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) ProcedureReturn EndIf sbResponseBody.i = CkStringBuilder::ckCreate() If sbResponseBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpResponse::ckGetBodySb(resp,sbResponseBody) jResp.i = CkJsonObject::ckCreate() If jResp.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(jResp,sbResponseBody) CkJsonObject::setCkEmitCompact(jResp, 0) Debug "Response Body:" Debug CkJsonObject::ckEmit(jResp) respStatusCode.i = CkHttpResponse::ckStatusCode(resp) Debug "Response Status Code = " + Str(respStatusCode) If respStatusCode >= 400 Debug "Response Header:" Debug CkHttpResponse::ckHeader(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) ; 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 date_created.i = CkDtObj::ckCreate() If date_created.i = 0 Debug "Failed to create object." ProcedureReturn EndIf date_updated.i = CkDtObj::ckCreate() If date_updated.i = 0 Debug "Failed to create object." ProcedureReturn EndIf name.s created_by.s idx.s contact_expire_after.s contacts_count.s id.s = CkJsonObject::ckStringOf(jResp,"id") first_name.s = CkJsonObject::ckStringOf(jResp,"first_name") last_name.s = CkJsonObject::ckStringOf(jResp,"last_name") phone_number.s = CkJsonObject::ckStringOf(jResp,"phone_number") email.s = CkJsonObject::ckStringOf(jResp,"email") gender.s = CkJsonObject::ckStringOf(jResp,"gender") city.s = CkJsonObject::ckStringOf(jResp,"city") CkJsonObject::ckDtOf(jResp,"date_created",0,date_created) CkJsonObject::ckDtOf(jResp,"date_updated",0,date_updated) description.s = CkJsonObject::ckStringOf(jResp,"description") i.i = 0 count_i.i = CkJsonObject::ckSizeOfArray(jResp,"groups") While i < count_i CkJsonObject::setCkI(jResp, i) id = CkJsonObject::ckStringOf(jResp,"groups[i].id") name = CkJsonObject::ckStringOf(jResp,"groups[i].name") CkJsonObject::ckDtOf(jResp,"groups[i].date_created",0,date_created) CkJsonObject::ckDtOf(jResp,"groups[i].date_updated",0,date_updated) description = CkJsonObject::ckStringOf(jResp,"groups[i].description") created_by = CkJsonObject::ckStringOf(jResp,"groups[i].created_by") idx = CkJsonObject::ckStringOf(jResp,"groups[i].idx") contact_expire_after = CkJsonObject::ckStringOf(jResp,"groups[i].contact_expire_after") contacts_count = CkJsonObject::ckStringOf(jResp,"groups[i].contacts_count") i = i + 1 Wend CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) CkDtObj::ckDispose(date_created) CkDtObj::ckDispose(date_updated) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.