Chilkat Online Tools

unicodeC / ShipEngine Walkthrough / Confidence scores

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *v_type;
    const wchar_t *text;
    int start_index;
    int end_index;
    const wchar_t *Value;
    int Line;
    const wchar_t *v_Type;
    const wchar_t *score;
    const wchar_t *Address_line1;
    const wchar_t *City_locality;
    const wchar_t *Postal_code;
    const wchar_t *Address_residential_indicator;
    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();

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "text": "I need to send a package to my friend Amanda. Her last name is Miller, btw. Her office address is 525 Winchester Blvd. That's in California for sure. Pretty sure it's San Jose, but I'm only like 90% sure about that. Pretty sure the zip code is something like 95128, or maybe 90210."
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"text",L"I need to send a package to my friend Amanda. Her last name is Miller, btw. Her office address is 525 Winchester Blvd. That's in California for sure. Pretty sure it's San Jose, but I'm only like 90% sure about that. Pretty sure the zip code is something like 95128, or maybe 90210.");

    CkHttpW_SetRequestHeader(http,L"API-Key",L"{{API_KEY}}");
    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");

    resp = CkHttpW_PostJson3(http,L"https://api.shipengine.com/v1/addresses/recognize",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "score": 0.7503696513496741,
    //   "address": {
    //     "address_line1": "525 Winchester Blvd",
    //     "city_locality": "Miller",
    //     "postal_code": "90210",
    //     "address_residential_indicator": "no"
    //   },
    //   "entities": [
    //     {
    //       "type": "city_locality",
    //       "score": 0.4792571878834418,
    //       "text": "Miller",
    //       "start_index": 63,
    //       "end_index": 68,
    //       "result": {
    //         "value": "Miller"
    //       }
    //     },
    //     {
    //       "type": "residential_indicator",
    //       "score": 0.9519646137063122,
    //       "text": "office",
    //       "start_index": 80,
    //       "end_index": 85,
    //       "result": {
    //         "value": "no"
    //       }
    //     },
    //     {
    //       "type": "address_line",
    //       "score": 0.6082904353940255,
    //       "text": "525 Winchester Blvd",
    //       "start_index": 98,
    //       "end_index": 116,
    //       "result": {
    //         "line": 1,
    //         "value": "525 Winchester Blvd"
    //       }
    //     },
    //     {
    //       "type": "number",
    //       "score": 0.9805313966503588,
    //       "text": "525",
    //       "start_index": 98,
    //       "end_index": 100,
    //       "result": {
    //         "type": "cardinal",
    //         "value": 525
    //       }
    //     },
    //     {
    //       "type": "city_locality",
    //       "score": 0.4792571878834418,
    //       "text": "San Jose",
    //       "start_index": 167,
    //       "end_index": 174,
    //       "result": {
    //         "value": "San Jose"
    //       }
    //     },
    //     {
    //       "type": "number",
    //       "score": 0.9805313966503588,
    //       "text": "90%",
    //       "start_index": 195,
    //       "end_index": 197,
    //       "result": {
    //         "type": "percentage",
    //         "value": 0.9
    //       }
    //     },
    //     {
    //       "type": "number",
    //       "score": 0.9805313966503588,
    //       "text": "95128",
    //       "start_index": 259,
    //       "end_index": 263,
    //       "result": {
    //         "type": "cardinal",
    //         "value": 95128
    //       }
    //     },
    //     {
    //       "type": "postal_code",
    //       "score": 0.9519646137063122,
    //       "text": "90210",
    //       "start_index": 275,
    //       "end_index": 279,
    //       "result": {
    //         "value": "90210"
    //       }
    //     }
    //   ]
    // }

    // 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.

    score = CkJsonObjectW_stringOf(jResp,L"score");
    Address_line1 = CkJsonObjectW_stringOf(jResp,L"address.address_line1");
    City_locality = CkJsonObjectW_stringOf(jResp,L"address.city_locality");
    Postal_code = CkJsonObjectW_stringOf(jResp,L"address.postal_code");
    Address_residential_indicator = CkJsonObjectW_stringOf(jResp,L"address.address_residential_indicator");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"entities");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        v_type = CkJsonObjectW_stringOf(jResp,L"entities[i].type");
        score = CkJsonObjectW_stringOf(jResp,L"entities[i].score");
        text = CkJsonObjectW_stringOf(jResp,L"entities[i].text");
        start_index = CkJsonObjectW_IntOf(jResp,L"entities[i].start_index");
        end_index = CkJsonObjectW_IntOf(jResp,L"entities[i].end_index");
        Value = CkJsonObjectW_stringOf(jResp,L"entities[i].result.value");
        Line = CkJsonObjectW_IntOf(jResp,L"entities[i].result.line");
        v_Type = CkJsonObjectW_stringOf(jResp,L"entities[i].result.type");
        i = i + 1;
    }



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

    }

Curl Command

curl -X POST
	-H "API-Key: {{API_KEY}}"
	-H "Content-Type: application/json"
	-d '{
	"text": "I need to send a package to my friend Amanda. Her last name is Miller, btw. Her office address is 525 Winchester Blvd. That\'s in California for sure. Pretty sure it\'s San Jose, but I\'m only like 90% sure about that. Pretty sure the zip code is something like 95128, or maybe 90210."
}'
https://api.shipengine.com/v1/addresses/recognize

Postman Collection Item JSON

{
  "name": "Confidence scores",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\t\"text\": \"I need to send a package to my friend Amanda. Her last name is Miller, btw. Her office address is 525 Winchester Blvd. That's in California for sure. Pretty sure it's San Jose, but I'm only like 90% sure about that. Pretty sure the zip code is something like 95128, or maybe 90210.\"\n}"
    },
    "url": {
      "raw": "https://api.shipengine.com/v1/addresses/recognize",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "addresses",
        "recognize"
      ]
    },
    "description": "ShipEngine's Parse Address API returns confidence scores for each entity that it recognizes, as well as for the address as a whole.  You can use these scores to determine whether the address is safe to use or requires additional validation or information from the user."
  },
  "response": [
    {
      "name": "Confidence scores",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\t\"text\": \"I need to send a package to my friend Amanda. Her last name is Miller, btw. Her office address is 525 Winchester Blvd. That's in California for sure. Pretty sure it's San Jose, but I'm only like 90% sure about that. Pretty sure the zip code is something like 95128, or maybe 90210.\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.shipengine.com/v1/addresses/recognize",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "addresses",
            "recognize"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Wed, 18 Sep 2019 16:18:21 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "content-encoding",
          "value": "gzip"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"score\": 0.7503696513496741,\n    \"address\": {\n        \"address_line1\": \"525 Winchester Blvd\",\n        \"city_locality\": \"Miller\",\n        \"postal_code\": \"90210\",\n        \"address_residential_indicator\": \"no\"\n    },\n    \"entities\": [\n        {\n            \"type\": \"city_locality\",\n            \"score\": 0.4792571878834418,\n            \"text\": \"Miller\",\n            \"start_index\": 63,\n            \"end_index\": 68,\n            \"result\": {\n                \"value\": \"Miller\"\n            }\n        },\n        {\n            \"type\": \"residential_indicator\",\n            \"score\": 0.9519646137063122,\n            \"text\": \"office\",\n            \"start_index\": 80,\n            \"end_index\": 85,\n            \"result\": {\n                \"value\": \"no\"\n            }\n        },\n        {\n            \"type\": \"address_line\",\n            \"score\": 0.6082904353940255,\n            \"text\": \"525 Winchester Blvd\",\n            \"start_index\": 98,\n            \"end_index\": 116,\n            \"result\": {\n                \"line\": 1,\n                \"value\": \"525 Winchester Blvd\"\n            }\n        },\n        {\n            \"type\": \"number\",\n            \"score\": 0.9805313966503588,\n            \"text\": \"525\",\n            \"start_index\": 98,\n            \"end_index\": 100,\n            \"result\": {\n                \"type\": \"cardinal\",\n                \"value\": 525\n            }\n        },\n        {\n            \"type\": \"city_locality\",\n            \"score\": 0.4792571878834418,\n            \"text\": \"San Jose\",\n            \"start_index\": 167,\n            \"end_index\": 174,\n            \"result\": {\n                \"value\": \"San Jose\"\n            }\n        },\n        {\n            \"type\": \"number\",\n            \"score\": 0.9805313966503588,\n            \"text\": \"90%\",\n            \"start_index\": 195,\n            \"end_index\": 197,\n            \"result\": {\n                \"type\": \"percentage\",\n                \"value\": 0.9\n            }\n        },\n        {\n            \"type\": \"number\",\n            \"score\": 0.9805313966503588,\n            \"text\": \"95128\",\n            \"start_index\": 259,\n            \"end_index\": 263,\n            \"result\": {\n                \"type\": \"cardinal\",\n                \"value\": 95128\n            }\n        },\n        {\n            \"type\": \"postal_code\",\n            \"score\": 0.9519646137063122,\n            \"text\": \"90210\",\n            \"start_index\": 275,\n            \"end_index\": 279,\n            \"result\": {\n                \"value\": \"90210\"\n            }\n        }\n    ]\n}"
    }
  ]
}