Chilkat Online Tools

unicodeC / ShipEngine Walkthrough / List a carrier's options

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *name;
    const wchar_t *default_value;
    const wchar_t *description;
    int i;
    int count_i;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    http = CkHttpW_Create();

    CkHttpW_SetRequestHeader(http,L"API-Key",L"{{API_KEY}}");

    sbResponseBody = CkStringBuilderW_Create();
    success = CkHttpW_QuickGetSb(http,L"https://api.shipengine.com/v1/carriers/se-963358/options",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:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "options": [
    //     {
    //       "name": "bill_to_account",
    //       "default_value": "",
    //       "description": ""
    //     },
    //     {
    //       "name": "bill_to_country_code",
    //       "default_value": "",
    //       "description": ""
    //     },
    //     {
    //       "name": "bill_to_party",
    //       "default_value": "",
    //       "description": ""
    //     },
    //     {
    //       "name": "bill_to_postal_code",
    //       "default_value": "",
    //       "description": ""
    //     },
    //     {
    //       "name": "contains_alcohol",
    //       "default_value": "false",
    //       "description": ""
    //     },
    //     {
    //       "name": "delivered_duty_paid",
    //       "default_value": "false",
    //       "description": ""
    //     },
    //     {
    //       "name": "dry_ice",
    //       "default_value": "false",
    //       "description": ""
    //     },
    //     {
    //       "name": "dry_ice_weight",
    //       "default_value": "0",
    //       "description": ""
    //     },
    //     {
    //       "name": "freight_class",
    //       "default_value": "",
    //       "description": ""
    //     },
    //     {
    //       "name": "non_machinable",
    //       "default_value": "false",
    //       "description": ""
    //     },
    //     {
    //       "name": "saturday_delivery",
    //       "default_value": "false",
    //       "description": ""
    //     }
    //   ]
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"options");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        name = CkJsonObjectW_stringOf(jResp,L"options[i].name");
        default_value = CkJsonObjectW_stringOf(jResp,L"options[i].default_value");
        description = CkJsonObjectW_stringOf(jResp,L"options[i].description");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X GET
	-H "API-Key: {{API_KEY}}"
https://api.shipengine.com/v1/carriers/se-963358/options

Postman Collection Item JSON

{
  "name": "List a carrier's options",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let response = pm.response.json();",
          "",
          "if (response.options.length > 0) {",
          "    const template = `",
          "        <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css\">",
          "        <link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\">",
          "        ",
          "        <h1>Carrier Options</h1>",
          "        <table class=\"table\" style=\"max-width: 600px\">",
          "            <thead class=\"thead-dark\">",
          "                <tr>",
          "                    <th>Option</th>",
          "                    <th>Default Value</th>",
          "                </tr>",
          "            </thead>",
          "            <tbody>",
          "                {{#each options}}",
          "                    <tr>",
          "                        <td>",
          "                            <samp>{{name}}</samp>",
          "                        </td>",
          "                        <td>{{default_value}}</td>",
          "                    </tr>",
          "                {{/each}}",
          "            </tbody>",
          "        </table>",
          "    `;",
          "    ",
          "    pm.visualizer.set(template, response);",
          "}"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "https://api.shipengine.com/v1/carriers/{{ups}}/options",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "carriers",
        "{{ups}}",
        "options"
      ]
    },
    "description": "Many carriers support \"advanced options\", such as the ability to ship alcohol, bill to the receiving party, request money on delivery, or many others.  This request shows you how to list the options that are supported by a carrier."
  },
  "response": [
    {
      "name": "List a carrier's options",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.shipengine.com/v1/carriers/{{ups}}/options",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "carriers",
            "{{ups}}",
            "options"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Tue, 17 Sep 2019 14:56:45 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "1174"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "54453653-caa0-4bb3-bcca-c86dd8a44317"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"options\": [\n        {\n            \"name\": \"bill_to_account\",\n            \"default_value\": \"\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"bill_to_country_code\",\n            \"default_value\": \"\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"bill_to_party\",\n            \"default_value\": \"\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"bill_to_postal_code\",\n            \"default_value\": \"\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"contains_alcohol\",\n            \"default_value\": \"false\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"delivered_duty_paid\",\n            \"default_value\": \"false\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"dry_ice\",\n            \"default_value\": \"false\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"dry_ice_weight\",\n            \"default_value\": \"0\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"freight_class\",\n            \"default_value\": \"\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"non_machinable\",\n            \"default_value\": \"false\",\n            \"description\": \"\"\n        },\n        {\n            \"name\": \"saturday_delivery\",\n            \"default_value\": \"false\",\n            \"description\": \"\"\n        }\n    ]\n}"
    }
  ]
}