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 <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
// 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
CkJsonObjectW queryParams;
queryParams.UpdateInt(L"limit",50);
queryParams.UpdateInt(L"page",1);
http.SetRequestHeader(L"X-FAIRE-ACCESS-TOKEN",L"<access_token>");
CkHttpResponseW resp;
success = http.HttpParams(L"GET",L"https://www.faire.com/api/v1/products",queryParams,resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
CkStringBuilderW sbResponseBody;
resp.GetBodySb(sbResponseBody);
CkJsonObjectW jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",jResp.emit());
int respStatusCode = resp.get_StatusCode();
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",resp.header());
wprintf(L"Failed.\n");
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.
const wchar_t *id = 0;
const wchar_t *brand_id = 0;
const wchar_t *short_description = 0;
const wchar_t *description = 0;
int wholesale_price_cents;
int retail_price_cents;
const wchar_t *sale_state = 0;
bool active;
bool deleted;
const wchar_t *name = 0;
int unit_multiplier;
const wchar_t *taxonomy_typeId = 0;
const wchar_t *taxonomy_typeName = 0;
const wchar_t *created_at = 0;
const wchar_t *updated_at = 0;
int j;
int count_j;
int page = jResp.IntOf(L"page");
int limit = jResp.IntOf(L"limit");
int i = 0;
int count_i = jResp.SizeOfArray(L"products");
while (i < count_i) {
jResp.put_I(i);
id = jResp.stringOf(L"products[i].id");
brand_id = jResp.stringOf(L"products[i].brand_id");
short_description = jResp.stringOf(L"products[i].short_description");
description = jResp.stringOf(L"products[i].description");
wholesale_price_cents = jResp.IntOf(L"products[i].wholesale_price_cents");
retail_price_cents = jResp.IntOf(L"products[i].retail_price_cents");
sale_state = jResp.stringOf(L"products[i].sale_state");
active = jResp.BoolOf(L"products[i].active");
deleted = jResp.BoolOf(L"products[i].deleted");
name = jResp.stringOf(L"products[i].name");
unit_multiplier = jResp.IntOf(L"products[i].unit_multiplier");
taxonomy_typeId = jResp.stringOf(L"products[i].taxonomy_type.id");
taxonomy_typeName = jResp.stringOf(L"products[i].taxonomy_type.name");
created_at = jResp.stringOf(L"products[i].created_at");
updated_at = jResp.stringOf(L"products[i].updated_at");
j = 0;
count_j = jResp.SizeOfArray(L"products[i].options");
while (j < count_j) {
jResp.put_J(j);
j = j + 1;
}
i = i + 1;
}
}