Sample code for 30+ languages & platforms
Java

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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new 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

        JsonObject queryParams = new JsonObject();
        queryParams.updateInt("limit", 50);
        queryParams.updateInt("page", 1);

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

        HttpResponse resp = new HttpResponse();
        try {
            http.httpParams("GET", "https://www.faire.com/api/v1/products", queryParams, resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);

        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("Failed.");
            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

        String id = "";
        String brand_id = "";
        String short_description = "";
        String description = "";
        int wholesale_price_cents = 0;
        int retail_price_cents = 0;
        String sale_state = "";
        boolean active = false;
        boolean deleted = false;
        String name = "";
        int unit_multiplier = 0;
        String taxonomy_typeId = "";
        String taxonomy_typeName = "";
        String created_at = "";
        String updated_at = "";
        int j = 0;
        int count_j = 0;

        int page = jResp.intOf("page");
        int limit = jResp.intOf("limit");
        int i = 0;
        int count_i = jResp.sizeOfArray("products");
        while (i < count_i) {
            jResp.setI(i);
            id = jResp.stringOf("products[i].id");
            brand_id = jResp.stringOf("products[i].brand_id");
            short_description = jResp.stringOf("products[i].short_description");
            description = jResp.stringOf("products[i].description");
            wholesale_price_cents = jResp.intOf("products[i].wholesale_price_cents");
            retail_price_cents = jResp.intOf("products[i].retail_price_cents");
            sale_state = jResp.stringOf("products[i].sale_state");
            active = jResp.boolOf("products[i].active");
            deleted = jResp.boolOf("products[i].deleted");
            name = jResp.stringOf("products[i].name");
            unit_multiplier = jResp.intOf("products[i].unit_multiplier");
            taxonomy_typeId = jResp.stringOf("products[i].taxonomy_type.id");
            taxonomy_typeName = jResp.stringOf("products[i].taxonomy_type.name");
            created_at = jResp.stringOf("products[i].created_at");
            updated_at = jResp.stringOf("products[i].updated_at");
            j = 0;
            count_j = jResp.sizeOfArray("products[i].options");
            while (j < count_j) {
                jResp.setJ(j);
                j = j + 1;
            }

            i = i + 1;
        }
    }
}