Chilkat Online Tools

PureBasic / ShipEngine Walkthrough / List a carrier's packaging

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"

Procedure ChilkatExample()

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::ckSetRequestHeader(http,"API-Key","{{API_KEY}}")

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckQuickGetSb(http,"https://api.shipengine.com/v1/carriers/se-963358/packages",sbResponseBody)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttp::ckLastStatus(http)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttp::ckLastHeader(http)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "packages": [
    ;     {
    ;       "package_id": null,
    ;       "package_code": "flat_rate_envelope",
    ;       "name": "Flat Rate Envelope",
    ;       "description": "USPS flat rate envelope. A special cardboard envelope provided by the USPS that clearly indicates \"Flat Rate\"."
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "flat_rate_legal_envelope",
    ;       "name": "Flat Rate Legal Envelope",
    ;       "description": "Flat Rate Legal Envelope"
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "flat_rate_padded_envelope",
    ;       "name": "Flat Rate Padded Envelope",
    ;       "description": "Flat Rate Padded Envelope"
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "large_envelope_or_flat",
    ;       "name": "Large Envelope or Flat",
    ;       "description": "Large envelope or flat. Has one dimension that is between 11 1/2\" and 15\" long, 6 1/18\" and 12\" high, or 1/4\" and 3/4\" thick."
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "large_flat_rate_box",
    ;       "name": "Large Flat Rate Box",
    ;       "description": "Large Flat Rate Box"
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "large_package",
    ;       "name": "Large Package (any side > 12\")",
    ;       "description": "Large package. Longest side plus the distance around the thickest part is over 84\" and less than or equal to 108\"."
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "letter",
    ;       "name": "Letter",
    ;       "description": "Letter"
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "medium_flat_rate_box",
    ;       "name": "Medium Flat Rate Box",
    ;       "description": "USPS flat rate box. A special 11\" x 8 1/2\" x 5 1/2\" or 14\" x 3.5\" x 12\" USPS box that clearly indicates \"Flat Rate Box\""
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "package",
    ;       "name": "Package",
    ;       "description": "Package. Longest side plus the distance around the thickest part is less than or equal to 84\""
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "regional_rate_box_a",
    ;       "name": "Regional Rate Box A",
    ;       "description": "Regional Rate Box A"
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "regional_rate_box_b",
    ;       "name": "Regional Rate Box B",
    ;       "description": "Regional Rate Box B"
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "small_flat_rate_box",
    ;       "name": "Small Flat Rate Box",
    ;       "description": "Small Flat Rate Box"
    ;     },
    ;     {
    ;       "package_id": null,
    ;       "package_code": "thick_envelope",
    ;       "name": "Thick Envelope",
    ;       "description": "Thick envelope. Envelopes or flats greater than 3/4\" at the thickest point."
    ;     }
    ;   ]
    ; }

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

    package_id.s
    package_code.s
    name.s
    description.s

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"packages")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        package_id = CkJsonObject::ckStringOf(jResp,"packages[i].package_id")
        package_code = CkJsonObject::ckStringOf(jResp,"packages[i].package_code")
        name = CkJsonObject::ckStringOf(jResp,"packages[i].name")
        description = CkJsonObject::ckStringOf(jResp,"packages[i].description")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

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

Postman Collection Item JSON

{
  "name": "List a carrier's packaging",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let response = pm.response.json();",
          "",
          "if (response.packages.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>Package Types</h1>",
          "        <table class=\"table\">",
          "            <thead class=\"thead-dark\">",
          "                <tr>",
          "                    <th>Package</th>",
          "                    <th>Code</th>",
          "                    <th>Description</th>",
          "                </tr>",
          "            </thead>",
          "            <tbody>",
          "                {{#each packages}}",
          "                    <tr>",
          "                        <td>{{name}}</td>",
          "                        <td>",
          "                            <samp>{{package_code}}</samp>",
          "                        </td>",
          "                        <td>{{description}}</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/{{stamps_com}}/packages",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "carriers",
        "{{stamps_com}}",
        "packages"
      ]
    },
    "description": "Most carriers have several different types of packaging, and which type you use can affect the cost of shipping a package.  This request shows you how to list the packaging that a specific carrier offers."
  },
  "response": [
    {
      "name": "List a carrier's packaging",
      "originalRequest": {
        "method": "GET",
        "header": [
        ],
        "url": {
          "raw": "https://api.shipengine.com/v1/carriers/{{stamps_com}}/packages",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "carriers",
            "{{stamps_com}}",
            "packages"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 16 Sep 2019 20:59:33 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "2749"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "server",
          "value": "Microsoft-IIS/8.0"
        },
        {
          "key": "x-newrelic-app-data",
          "value": "PxQGVFZXCgITVVZbAwMCV1YGFB9AMQYAZBBZDEtZV0ZaCldOZgRRIzR/GCIHEUpaVRYSTXtYRBI0UFVYAwEGERJOCEwICAQLC1IOTQFPA1BWBw5WVE8VAhxGViB1Ag5RU3QGcFRTWld2VBpOXllYQVY4"
        },
        {
          "key": "x-powered-by",
          "value": "ASP.NET"
        },
        {
          "key": "x-shipengine-environment",
          "value": "j"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "ed9ca71b-b641-44b9-a8a5-d4d209d52660"
        },
        {
          "key": "x-shipengine-server",
          "value": "SS-J-WEB03"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"packages\": [\n        {\n            \"package_id\": null,\n            \"package_code\": \"flat_rate_envelope\",\n            \"name\": \"Flat Rate Envelope\",\n            \"description\": \"USPS flat rate envelope. A special cardboard envelope provided by the USPS that clearly indicates \\\"Flat Rate\\\".\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"flat_rate_legal_envelope\",\n            \"name\": \"Flat Rate Legal Envelope\",\n            \"description\": \"Flat Rate Legal Envelope\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"flat_rate_padded_envelope\",\n            \"name\": \"Flat Rate Padded Envelope\",\n            \"description\": \"Flat Rate Padded Envelope\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"large_envelope_or_flat\",\n            \"name\": \"Large Envelope or Flat\",\n            \"description\": \"Large envelope or flat. Has one dimension that is between 11 1/2\\\" and 15\\\" long, 6 1/18\\\" and 12\\\" high, or 1/4\\\" and 3/4\\\" thick.\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"large_flat_rate_box\",\n            \"name\": \"Large Flat Rate Box\",\n            \"description\": \"Large Flat Rate Box\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"large_package\",\n            \"name\": \"Large Package (any side > 12\\\")\",\n            \"description\": \"Large package. Longest side plus the distance around the thickest part is over 84\\\" and less than or equal to 108\\\".\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"letter\",\n            \"name\": \"Letter\",\n            \"description\": \"Letter\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"medium_flat_rate_box\",\n            \"name\": \"Medium Flat Rate Box\",\n            \"description\": \"USPS flat rate box. A special 11\\\" x 8 1/2\\\" x 5 1/2\\\" or 14\\\" x 3.5\\\" x 12\\\" USPS box that clearly indicates \\\"Flat Rate Box\\\"\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"package\",\n            \"name\": \"Package\",\n            \"description\": \"Package. Longest side plus the distance around the thickest part is less than or equal to 84\\\"\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"regional_rate_box_a\",\n            \"name\": \"Regional Rate Box A\",\n            \"description\": \"Regional Rate Box A\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"regional_rate_box_b\",\n            \"name\": \"Regional Rate Box B\",\n            \"description\": \"Regional Rate Box B\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"small_flat_rate_box\",\n            \"name\": \"Small Flat Rate Box\",\n            \"description\": \"Small Flat Rate Box\"\n        },\n        {\n            \"package_id\": null,\n            \"package_code\": \"thick_envelope\",\n            \"name\": \"Thick Envelope\",\n            \"description\": \"Thick envelope. Envelopes or flats greater than 3/4\\\" at the thickest point.\"\n        }\n    ]\n}"
    }
  ]
}