Unicode C
Unicode C
GetHarvest - List Clients
See more GetHarvest Examples
Returns a list of your clients.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
int per_page;
int total_pages;
int total_entries;
const wchar_t *next_page;
const wchar_t *previous_page;
int page;
const wchar_t *linksFirst;
const wchar_t *linksNext;
const wchar_t *linksPrevious;
const wchar_t *linksLast;
int i;
int count_i;
int id;
const wchar_t *name;
BOOL is_active;
const wchar_t *address;
const wchar_t *created_at;
const wchar_t *updated_at;
const wchar_t *currency;
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 "https://api.harvestapp.com/v2/clients" \
// -H "Authorization: Bearer ACCESS_TOKEN" \
// -H "Harvest-Account-Id: ACCOUNT_ID" \
// -H "User-Agent: MyApp (yourname@example.com)"
CkHttpW_SetRequestHeader(http,L"User-Agent",L"MyApp (yourname@example.com)");
CkHttpW_SetRequestHeader(http,L"Authorization",L"Bearer ACCESS_TOKEN");
CkHttpW_SetRequestHeader(http,L"Harvest-Account-Id",L"ACCOUNT_ID");
sbResponseBody = CkStringBuilderW_Create();
success = CkHttpW_QuickGetSb(http,L"https://api.harvestapp.com/v2/clients",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:
// {
// "clients": [
// {
// "id": 5735776,
// "name": "123 Industries",
// "is_active": true,
// "address": "123 Main St.\r\nAnytown, LA 71223",
// "created_at": "2017-06-26T21:02:12Z",
// "updated_at": "2017-06-26T21:34:11Z",
// "currency": "EUR"
// },
// {
// "id": 5735774,
// "name": "ABC Corp",
// "is_active": true,
// "address": "456 Main St.\r\nAnytown, CT 06467",
// "created_at": "2017-06-26T21:01:52Z",
// "updated_at": "2017-06-26T21:27:07Z",
// "currency": "USD"
// }
// ],
// "per_page": 100,
// "total_pages": 1,
// "total_entries": 2,
// "next_page": null,
// "previous_page": null,
// "page": 1,
// "links": {
// "first": "https://api.harvestapp.com/v2/clients?page=1&per_page=100",
// "next": null,
// "previous": null,
// "last": "https://api.harvestapp.com/v2/clients?page=1&per_page=100"
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
per_page = CkJsonObjectW_IntOf(jResp,L"per_page");
total_pages = CkJsonObjectW_IntOf(jResp,L"total_pages");
total_entries = CkJsonObjectW_IntOf(jResp,L"total_entries");
next_page = CkJsonObjectW_stringOf(jResp,L"next_page");
previous_page = CkJsonObjectW_stringOf(jResp,L"previous_page");
page = CkJsonObjectW_IntOf(jResp,L"page");
linksFirst = CkJsonObjectW_stringOf(jResp,L"links.first");
linksNext = CkJsonObjectW_stringOf(jResp,L"links.next");
linksPrevious = CkJsonObjectW_stringOf(jResp,L"links.previous");
linksLast = CkJsonObjectW_stringOf(jResp,L"links.last");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"clients");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
id = CkJsonObjectW_IntOf(jResp,L"clients[i].id");
name = CkJsonObjectW_stringOf(jResp,L"clients[i].name");
is_active = CkJsonObjectW_BoolOf(jResp,L"clients[i].is_active");
address = CkJsonObjectW_stringOf(jResp,L"clients[i].address");
created_at = CkJsonObjectW_stringOf(jResp,L"clients[i].created_at");
updated_at = CkJsonObjectW_stringOf(jResp,L"clients[i].updated_at");
currency = CkJsonObjectW_stringOf(jResp,L"clients[i].currency");
i = i + 1;
}
CkHttpW_Dispose(http);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}