Unicode C
Unicode C
SMSAPI - List Contacts
See more SMSAPI Examples
List ContactsChilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkDtObjW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
HCkDtObjW date_created;
HCkDtObjW date_updated;
const wchar_t *id;
const wchar_t *first_name;
const wchar_t *last_name;
const wchar_t *phone_number;
const wchar_t *email;
const wchar_t *gender;
const wchar_t *city;
const wchar_t *country;
const wchar_t *source;
int j;
int count_j;
const wchar_t *name;
const wchar_t *description;
const wchar_t *created_by;
const wchar_t *idx;
const wchar_t *contact_expire_after;
const wchar_t *contacts_count;
int size;
int i;
int count_i;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// Implements the following CURL command:
// curl -X GET -H "Authorization: Bearer token_api_oauth" https://api.smsapi.com/contacts?phone_number=48500000000
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Adds the "Authorization: Bearer token_api_oauth" header.
CkHttpW_putAuthToken(http,L"token_api_oauth");
sbResponseBody = CkStringBuilderW_Create();
success = CkHttpW_QuickGetSb(http,L"https://api.smsapi.com/contacts?phone_number=48500000000",sbResponseBody);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
return;
}
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpW_getLastStatus(http);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpW_lastHeader(http));
wprintf(L"Failed.\n");
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "size": 1,
// "collection": [
// {
// "id": "5b83ba81a788494a0469490f",
// "first_name": "name",
// "last_name": "surname",
// "phone_number": "48500000000",
// "email": "bok@smsapi.com",
// "gender": "male",
// "city": "City",
// "country": "Poland",
// "source": "source",
// "date_created": "2018-08-27T10:46:57+02:00",
// "date_updated": "2018-08-27T10:46:57+02:00",
// "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": "login",
// "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
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
date_created = CkDtObjW_Create();
date_updated = CkDtObjW_Create();
size = CkJsonObjectW_IntOf(jResp,L"size");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"collection");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
id = CkJsonObjectW_stringOf(jResp,L"collection[i].id");
first_name = CkJsonObjectW_stringOf(jResp,L"collection[i].first_name");
last_name = CkJsonObjectW_stringOf(jResp,L"collection[i].last_name");
phone_number = CkJsonObjectW_stringOf(jResp,L"collection[i].phone_number");
email = CkJsonObjectW_stringOf(jResp,L"collection[i].email");
gender = CkJsonObjectW_stringOf(jResp,L"collection[i].gender");
city = CkJsonObjectW_stringOf(jResp,L"collection[i].city");
country = CkJsonObjectW_stringOf(jResp,L"collection[i].country");
source = CkJsonObjectW_stringOf(jResp,L"collection[i].source");
CkJsonObjectW_DtOf(jResp,L"collection[i].date_created",FALSE,date_created);
CkJsonObjectW_DtOf(jResp,L"collection[i].date_updated",FALSE,date_updated);
j = 0;
count_j = CkJsonObjectW_SizeOfArray(jResp,L"collection[i].groups");
while (j < count_j) {
CkJsonObjectW_putJ(jResp,j);
id = CkJsonObjectW_stringOf(jResp,L"collection[i].groups[j].id");
name = CkJsonObjectW_stringOf(jResp,L"collection[i].groups[j].name");
CkJsonObjectW_DtOf(jResp,L"collection[i].groups[j].date_created",FALSE,date_created);
CkJsonObjectW_DtOf(jResp,L"collection[i].groups[j].date_updated",FALSE,date_updated);
description = CkJsonObjectW_stringOf(jResp,L"collection[i].groups[j].description");
created_by = CkJsonObjectW_stringOf(jResp,L"collection[i].groups[j].created_by");
idx = CkJsonObjectW_stringOf(jResp,L"collection[i].groups[j].idx");
contact_expire_after = CkJsonObjectW_stringOf(jResp,L"collection[i].groups[j].contact_expire_after");
contacts_count = CkJsonObjectW_stringOf(jResp,L"collection[i].groups[j].contacts_count");
j = j + 1;
}
i = i + 1;
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
CkDtObjW_Dispose(date_created);
CkDtObjW_Dispose(date_updated);
}