Unicode C
Unicode C
SMSAPI - Create a Contact
See more SMSAPI Examples
Create a ContactChilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkHttpRequestW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkDtObjW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkHttpRequestW req;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
HCkDtObjW date_created;
HCkDtObjW date_updated;
const wchar_t *name;
const wchar_t *created_by;
const wchar_t *idx;
const wchar_t *contact_expire_after;
const wchar_t *contacts_count;
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 *description;
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 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 = CkHttpRequestW_Create();
CkHttpRequestW_putHttpVerb(req,L"POST");
CkHttpRequestW_putPath(req,L"/contacts");
CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded");
CkHttpRequestW_AddParam(req,L"phone_number",L"48500000000");
CkHttpRequestW_AddParam(req,L"email",L"bok@smsapi.com");
CkHttpRequestW_AddParam(req,L"first_name",L"Name");
CkHttpRequestW_AddParam(req,L"last_name",L"Last_name");
CkHttpRequestW_AddParam(req,L"gender",L"gender");
CkHttpRequestW_AddParam(req,L"description",L"description");
CkHttpRequestW_AddParam(req,L"city",L"city");
CkHttpRequestW_AddParam(req,L"groups",L"default");
CkHttpRequestW_AddHeader(req,L"Authorization",L"Bearer token_api_oauth");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpReq(http,L"https://api.smsapi.com/contacts",req,resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
CkHttpResponseW_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilderW_Create();
CkHttpResponseW_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpResponseW_getStatusCode(resp);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpResponseW_header(resp));
wprintf(L"Failed.\n");
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// 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
// 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();
id = CkJsonObjectW_stringOf(jResp,L"id");
first_name = CkJsonObjectW_stringOf(jResp,L"first_name");
last_name = CkJsonObjectW_stringOf(jResp,L"last_name");
phone_number = CkJsonObjectW_stringOf(jResp,L"phone_number");
email = CkJsonObjectW_stringOf(jResp,L"email");
gender = CkJsonObjectW_stringOf(jResp,L"gender");
city = CkJsonObjectW_stringOf(jResp,L"city");
CkJsonObjectW_DtOf(jResp,L"date_created",FALSE,date_created);
CkJsonObjectW_DtOf(jResp,L"date_updated",FALSE,date_updated);
description = CkJsonObjectW_stringOf(jResp,L"description");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"groups");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
id = CkJsonObjectW_stringOf(jResp,L"groups[i].id");
name = CkJsonObjectW_stringOf(jResp,L"groups[i].name");
CkJsonObjectW_DtOf(jResp,L"groups[i].date_created",FALSE,date_created);
CkJsonObjectW_DtOf(jResp,L"groups[i].date_updated",FALSE,date_updated);
description = CkJsonObjectW_stringOf(jResp,L"groups[i].description");
created_by = CkJsonObjectW_stringOf(jResp,L"groups[i].created_by");
idx = CkJsonObjectW_stringOf(jResp,L"groups[i].idx");
contact_expire_after = CkJsonObjectW_stringOf(jResp,L"groups[i].contact_expire_after");
contacts_count = CkJsonObjectW_stringOf(jResp,L"groups[i].contacts_count");
i = i + 1;
}
CkHttpW_Dispose(http);
CkHttpRequestW_Dispose(req);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
CkDtObjW_Dispose(date_created);
CkDtObjW_Dispose(date_updated);
}