Sample code for 30+ languages & platforms
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 C Downloads

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttp http;
    HCkJsonObject queryParams;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *id;
    const char *brand_id;
    const char *short_description;
    const char *description;
    int wholesale_price_cents;
    int retail_price_cents;
    const char *sale_state;
    BOOL active;
    BOOL deleted;
    const char *name;
    int unit_multiplier;
    const char *taxonomy_typeId;
    const char *taxonomy_typeName;
    const char *created_at;
    const char *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 = CkHttp_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 = CkJsonObject_Create();
    CkJsonObject_UpdateInt(queryParams,"limit",50);
    CkJsonObject_UpdateInt(queryParams,"page",1);

    CkHttp_SetRequestHeader(http,"X-FAIRE-ACCESS-TOKEN","<access_token>");

    resp = CkHttpResponse_Create();
    success = CkHttp_HttpParams(http,"GET","https://www.faire.com/api/v1/products",queryParams,resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(queryParams);
        CkHttpResponse_Dispose(resp);
        return;
    }

    sbResponseBody = CkStringBuilder_Create();
    CkHttpResponse_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(queryParams);
        CkHttpResponse_Dispose(resp);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_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 = CkJsonObject_IntOf(jResp,"page");
    limit = CkJsonObject_IntOf(jResp,"limit");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"products");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        id = CkJsonObject_stringOf(jResp,"products[i].id");
        brand_id = CkJsonObject_stringOf(jResp,"products[i].brand_id");
        short_description = CkJsonObject_stringOf(jResp,"products[i].short_description");
        description = CkJsonObject_stringOf(jResp,"products[i].description");
        wholesale_price_cents = CkJsonObject_IntOf(jResp,"products[i].wholesale_price_cents");
        retail_price_cents = CkJsonObject_IntOf(jResp,"products[i].retail_price_cents");
        sale_state = CkJsonObject_stringOf(jResp,"products[i].sale_state");
        active = CkJsonObject_BoolOf(jResp,"products[i].active");
        deleted = CkJsonObject_BoolOf(jResp,"products[i].deleted");
        name = CkJsonObject_stringOf(jResp,"products[i].name");
        unit_multiplier = CkJsonObject_IntOf(jResp,"products[i].unit_multiplier");
        taxonomy_typeId = CkJsonObject_stringOf(jResp,"products[i].taxonomy_type.id");
        taxonomy_typeName = CkJsonObject_stringOf(jResp,"products[i].taxonomy_type.name");
        created_at = CkJsonObject_stringOf(jResp,"products[i].created_at");
        updated_at = CkJsonObject_stringOf(jResp,"products[i].updated_at");
        j = 0;
        count_j = CkJsonObject_SizeOfArray(jResp,"products[i].options");
        while (j < count_j) {
            CkJsonObject_putJ(jResp,j);
            j = j + 1;
        }

        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(queryParams);
    CkHttpResponse_Dispose(resp);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }