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
(Delphi DLL) SMSAPI - Create a ContactCreate a Contact For more information, see https://www.smsapi.com/docs?shell#create-contact
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, DtObj, HttpResponse, StringBuilder, JsonObject, HttpRequest; ... procedure TForm1.Button1Click(Sender: TObject); var http: HCkHttp; success: Boolean; req: HCkHttpRequest; resp: HCkHttpResponse; sbResponseBody: HCkStringBuilder; jResp: HCkJsonObject; respStatusCode: Integer; date_created: HCkDtObj; date_updated: HCkDtObj; name: PWideChar; created_by: PWideChar; idx: PWideChar; contact_expire_after: PWideChar; contacts_count: PWideChar; id: PWideChar; first_name: PWideChar; last_name: PWideChar; phone_number: PWideChar; email: PWideChar; gender: PWideChar; city: PWideChar; description: PWideChar; i: Integer; count_i: Integer; begin // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http := CkHttp_Create(); // 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 := CkHttpRequest_Create(); CkHttpRequest_putHttpVerb(req,'POST'); CkHttpRequest_putPath(req,'/contacts'); CkHttpRequest_putContentType(req,'application/x-www-form-urlencoded'); CkHttpRequest_AddParam(req,'phone_number','48500000000'); CkHttpRequest_AddParam(req,'email','bok@smsapi.com'); CkHttpRequest_AddParam(req,'first_name','Name'); CkHttpRequest_AddParam(req,'last_name','Last_name'); CkHttpRequest_AddParam(req,'gender','gender'); CkHttpRequest_AddParam(req,'description','description'); CkHttpRequest_AddParam(req,'city','city'); CkHttpRequest_AddParam(req,'groups','default'); CkHttpRequest_AddHeader(req,'Authorization','Bearer token_api_oauth'); resp := CkHttp_PostUrlEncoded(http,'https://api.smsapi.com/contacts',req); if (CkHttp_getLastMethodSuccess(http) = False) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; sbResponseBody := CkStringBuilder_Create(); CkHttpResponse_GetBodySb(resp,sbResponseBody); jResp := CkJsonObject_Create(); CkJsonObject_LoadSb(jResp,sbResponseBody); CkJsonObject_putEmitCompact(jResp,False); Memo1.Lines.Add('Response Body:'); Memo1.Lines.Add(CkJsonObject__emit(jResp)); respStatusCode := CkHttpResponse_getStatusCode(resp); Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode)); if (respStatusCode >= 400) then begin Memo1.Lines.Add('Response Header:'); Memo1.Lines.Add(CkHttpResponse__header(resp)); Memo1.Lines.Add('Failed.'); CkHttpResponse_Dispose(resp); Exit; end; CkHttpResponse_Dispose(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 := CkDtObj_Create(); date_updated := CkDtObj_Create(); id := CkJsonObject__stringOf(jResp,'id'); first_name := CkJsonObject__stringOf(jResp,'first_name'); last_name := CkJsonObject__stringOf(jResp,'last_name'); phone_number := CkJsonObject__stringOf(jResp,'phone_number'); email := CkJsonObject__stringOf(jResp,'email'); gender := CkJsonObject__stringOf(jResp,'gender'); city := CkJsonObject__stringOf(jResp,'city'); CkJsonObject_DtOf(jResp,'date_created',False,date_created); CkJsonObject_DtOf(jResp,'date_updated',False,date_updated); description := CkJsonObject__stringOf(jResp,'description'); i := 0; count_i := CkJsonObject_SizeOfArray(jResp,'groups'); while i < count_i do begin CkJsonObject_putI(jResp,i); id := CkJsonObject__stringOf(jResp,'groups[i].id'); name := CkJsonObject__stringOf(jResp,'groups[i].name'); CkJsonObject_DtOf(jResp,'groups[i].date_created',False,date_created); CkJsonObject_DtOf(jResp,'groups[i].date_updated',False,date_updated); description := CkJsonObject__stringOf(jResp,'groups[i].description'); created_by := CkJsonObject__stringOf(jResp,'groups[i].created_by'); idx := CkJsonObject__stringOf(jResp,'groups[i].idx'); contact_expire_after := CkJsonObject__stringOf(jResp,'groups[i].contact_expire_after'); contacts_count := CkJsonObject__stringOf(jResp,'groups[i].contacts_count'); i := i + 1; end; CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jResp); CkDtObj_Dispose(date_created); CkDtObj_Dispose(date_updated); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.