Unicode C
Unicode C
Faire - Get All Products
See more Faire Examples
Retrieves a list of products, ordered ascending by updated_at. By default, it only returns non-deleted products.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkJsonObjectW queryParams;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *id;
const wchar_t *brand_id;
const wchar_t *short_description;
const wchar_t *description;
int wholesale_price_cents;
int retail_price_cents;
const wchar_t *sale_state;
BOOL active;
BOOL deleted;
const wchar_t *name;
int unit_multiplier;
const wchar_t *taxonomy_typeId;
const wchar_t *taxonomy_typeName;
const wchar_t *created_at;
const wchar_t *updated_at;
int j;
int count_j;
int page;
int limit;
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 "X-FAIRE-ACCESS-TOKEN: <access_token>" -d "limit=50" -d "page=1" https://www.faire.com/api/v1/products
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
queryParams = CkJsonObjectW_Create();
CkJsonObjectW_UpdateInt(queryParams,L"limit",50);
CkJsonObjectW_UpdateInt(queryParams,L"page",1);
CkHttpW_SetRequestHeader(http,L"X-FAIRE-ACCESS-TOKEN",L"<access_token>");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpParams(http,L"GET",L"https://www.faire.com/api/v1/products",queryParams,resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(queryParams);
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);
CkJsonObjectW_Dispose(queryParams);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "page": 1,
// "limit": 50,
// "products": [
// {
// "id": "p_123",
// "brand_id": "b_abc",
// "short_description": "Our candles smell fantastic. Want to know how good? Read our description!",
// "description": "Glad you decided to read our description! We have significantly more characters to describe to you just how good our candles smell.",
// "wholesale_price_cents": 500,
// "retail_price_cents": 1000,
// "sale_state": "FOR_SALE",
// "active": true,
// "deleted": false,
// "name": "Faire's fantastic candle",
// "unit_multiplier": 8,
// "taxonomy_type": {
// "id": "tt_23nl3bzl00",
// "name": "Votive Candle"
// },
// "options": [
// ],
// "created_at": "20190314T000915.000Z",
// "updated_at": "20190315T000915.000Z"
// }
// ]
// }
// 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.
page = CkJsonObjectW_IntOf(jResp,L"page");
limit = CkJsonObjectW_IntOf(jResp,L"limit");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"products");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
id = CkJsonObjectW_stringOf(jResp,L"products[i].id");
brand_id = CkJsonObjectW_stringOf(jResp,L"products[i].brand_id");
short_description = CkJsonObjectW_stringOf(jResp,L"products[i].short_description");
description = CkJsonObjectW_stringOf(jResp,L"products[i].description");
wholesale_price_cents = CkJsonObjectW_IntOf(jResp,L"products[i].wholesale_price_cents");
retail_price_cents = CkJsonObjectW_IntOf(jResp,L"products[i].retail_price_cents");
sale_state = CkJsonObjectW_stringOf(jResp,L"products[i].sale_state");
active = CkJsonObjectW_BoolOf(jResp,L"products[i].active");
deleted = CkJsonObjectW_BoolOf(jResp,L"products[i].deleted");
name = CkJsonObjectW_stringOf(jResp,L"products[i].name");
unit_multiplier = CkJsonObjectW_IntOf(jResp,L"products[i].unit_multiplier");
taxonomy_typeId = CkJsonObjectW_stringOf(jResp,L"products[i].taxonomy_type.id");
taxonomy_typeName = CkJsonObjectW_stringOf(jResp,L"products[i].taxonomy_type.name");
created_at = CkJsonObjectW_stringOf(jResp,L"products[i].created_at");
updated_at = CkJsonObjectW_stringOf(jResp,L"products[i].updated_at");
j = 0;
count_j = CkJsonObjectW_SizeOfArray(jResp,L"products[i].options");
while (j < count_j) {
CkJsonObjectW_putJ(jResp,j);
j = j + 1;
}
i = i + 1;
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(queryParams);
CkHttpResponseW_Dispose(resp);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}