Chilkat Online Tools

unicodeC / ShipEngine Walkthrough / List a carrier's services

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 *carrier_id;
    const wchar_t *carrier_code;
    const wchar_t *service_code;
    const wchar_t *name;
    BOOL domestic;
    BOOL international;
    BOOL is_multi_package_supported;
    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-963360/services",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)

    // {
    //   "services": [
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_first_class_mail",
    //       "name": "USPS First Class Mail",
    //       "domestic": true,
    //       "international": false,
    //       "is_multi_package_supported": false
    //     },
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_media_mail",
    //       "name": "USPS Media Mail",
    //       "domestic": true,
    //       "international": false,
    //       "is_multi_package_supported": false
    //     },
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_parcel_select",
    //       "name": "USPS Parcel Select Ground",
    //       "domestic": true,
    //       "international": false,
    //       "is_multi_package_supported": false
    //     },
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_priority_mail",
    //       "name": "USPS Priority Mail",
    //       "domestic": true,
    //       "international": false,
    //       "is_multi_package_supported": false
    //     },
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_priority_mail_express",
    //       "name": "USPS Priority Mail Express",
    //       "domestic": true,
    //       "international": false,
    //       "is_multi_package_supported": false
    //     },
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_first_class_mail_international",
    //       "name": "USPS First Class Mail Intl",
    //       "domestic": false,
    //       "international": true,
    //       "is_multi_package_supported": false
    //     },
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_priority_mail_international",
    //       "name": "USPS Priority Mail Intl",
    //       "domestic": false,
    //       "international": true,
    //       "is_multi_package_supported": false
    //     },
    //     {
    //       "carrier_id": "se-121861",
    //       "carrier_code": "stamps_com",
    //       "service_code": "usps_priority_mail_express_international",
    //       "name": "USPS Priority Mail Express Intl",
    //       "domestic": false,
    //       "international": true,
    //       "is_multi_package_supported": false
    //     }
    //   ]
    // }

    // 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"services");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        carrier_id = CkJsonObjectW_stringOf(jResp,L"services[i].carrier_id");
        carrier_code = CkJsonObjectW_stringOf(jResp,L"services[i].carrier_code");
        service_code = CkJsonObjectW_stringOf(jResp,L"services[i].service_code");
        name = CkJsonObjectW_stringOf(jResp,L"services[i].name");
        domestic = CkJsonObjectW_BoolOf(jResp,L"services[i].domestic");
        international = CkJsonObjectW_BoolOf(jResp,L"services[i].international");
        is_multi_package_supported = CkJsonObjectW_BoolOf(jResp,L"services[i].is_multi_package_supported");
        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-963360/services

Postman Collection Item JSON

{
  "name": "List a carrier's services",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let response = pm.response.json();",
          "",
          "if (response.services.length > 0) {",
          "    response.carrier_code = response.services[0].carrier_code;",
          "",
          "    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>",
          "            <img src=\"https://www.shipengine.com/docs/img/logos/carriers/{{carrier_code}}.svg\" class=\"mr-3\" style=\"height: 50px\">",
          "            Services Offered",
          "        </h1>",
          "        <table class=\"table\">",
          "            <thead class=\"thead-dark\">",
          "                <tr>",
          "                    <th>Service</th>",
          "                    <th>Code</th>",
          "                    <th>Regions</th>",
          "                </tr>",
          "            </thead>",
          "            <tbody>",
          "                {{#each services}}",
          "                    <tr>",
          "                        <td>{{name}}</td>",
          "                        <td>",
          "                            <samp>{{service_code}}</samp>",
          "                        </td>",
          "                        <td>",
          "                            {{#if domestic}}✔{{else}}❌{{/if}} Domestic<br>",
          "                            {{#if international}}✔{{else}}❌{{/if}} International",
          "                        </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/{{fedex}}/services",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "carriers",
        "{{fedex}}",
        "services"
      ]
    },
    "description": "Each carrier supports many different services, such as ground, air, 3-day, overnight, etc.  In ShipEngine, each of these services has a `service_code`, which you'll need to specify when shipping a package.  This request shows you how to list all the available services (and their service codes) for a carrier."
  },
  "response": [
    {
      "name": "List a carrier's services",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.shipengine.com/v1/carriers/{{stamps_com}}/services",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "carriers",
            "{{stamps_com}}",
            "services"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 16 Sep 2019 20:59:12 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "2263"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "ddbbd84a-03ab-48e8-8fc1-119489aae2b7"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"services\": [\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_first_class_mail\",\n            \"name\": \"USPS First Class Mail\",\n            \"domestic\": true,\n            \"international\": false,\n            \"is_multi_package_supported\": false\n        },\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_media_mail\",\n            \"name\": \"USPS Media Mail\",\n            \"domestic\": true,\n            \"international\": false,\n            \"is_multi_package_supported\": false\n        },\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_parcel_select\",\n            \"name\": \"USPS Parcel Select Ground\",\n            \"domestic\": true,\n            \"international\": false,\n            \"is_multi_package_supported\": false\n        },\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_priority_mail\",\n            \"name\": \"USPS Priority Mail\",\n            \"domestic\": true,\n            \"international\": false,\n            \"is_multi_package_supported\": false\n        },\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_priority_mail_express\",\n            \"name\": \"USPS Priority Mail Express\",\n            \"domestic\": true,\n            \"international\": false,\n            \"is_multi_package_supported\": false\n        },\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_first_class_mail_international\",\n            \"name\": \"USPS First Class Mail Intl\",\n            \"domestic\": false,\n            \"international\": true,\n            \"is_multi_package_supported\": false\n        },\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_priority_mail_international\",\n            \"name\": \"USPS Priority Mail Intl\",\n            \"domestic\": false,\n            \"international\": true,\n            \"is_multi_package_supported\": false\n        },\n        {\n            \"carrier_id\": \"se-121861\",\n            \"carrier_code\": \"stamps_com\",\n            \"service_code\": \"usps_priority_mail_express_international\",\n            \"name\": \"USPS Priority Mail Express Intl\",\n            \"domestic\": false,\n            \"international\": true,\n            \"is_multi_package_supported\": false\n        }\n    ]\n}"
    }
  ]
}