Chilkat Online Tools

SQL Server / ShipEngine Walkthrough / Already-known fields

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- 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 Miller’s house at 525 Winchester Blvd in San Jose (that's california, obviously). The zip code there is 95128.",
    --   "address": {
    --     "country_code": "US"
    --   }
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'text', 'I need to send a package to my friend Amanda Miller’s house at 525 Winchester Blvd in San Jose (that''s california, obviously). The zip code there is 95128.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'address.country_code', 'US'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'API-Key', '{{API_KEY}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.shipengine.com/v1/addresses/recognize', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

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

    -- {
    --   "score": 0.9710697187242877,
    --   "address": {
    --     "name": "Amanda Miller",
    --     "address_line1": "525 Winchester Blvd",
    --     "city_locality": "San Jose",
    --     "state_province": "CA",
    --     "postal_code": "95128",
    --     "country_code": "US",
    --     "address_residential_indicator": "yes"
    --   },
    --   "entities": [
    --     {
    --       "type": "person",
    --       "score": 0.9519646137063122,
    --       "text": "Amanda Miller",
    --       "start_index": 38,
    --       "end_index": 50,
    --       "result": {
    --         "value": "Amanda Miller"
    --       }
    --     },
    --     {
    --       "type": "residential_indicator",
    --       "score": 0.9519646137063122,
    --       "text": "house",
    --       "start_index": 54,
    --       "end_index": 58,
    --       "result": {
    --         "value": "yes"
    --       }
    --     },
    --     {
    --       "type": "address_line",
    --       "score": 0.9805313966503588,
    --       "text": "525 Winchester Blvd",
    --       "start_index": 63,
    --       "end_index": 81,
    --       "result": {
    --         "line": 1,
    --         "value": "525 Winchester Blvd"
    --       }
    --     },
    --     {
    --       "type": "number",
    --       "score": 0.9805313966503588,
    --       "text": "525",
    --       "start_index": 63,
    --       "end_index": 65,
    --       "result": {
    --         "type": "cardinal",
    --         "value": 525
    --       }
    --     },
    --     {
    --       "type": "city_locality",
    --       "score": 0.9805313966503588,
    --       "text": "San Jose",
    --       "start_index": 86,
    --       "end_index": 93,
    --       "result": {
    --         "value": "San Jose"
    --       }
    --     },
    --     {
    --       "type": "state_province",
    --       "score": 0.9805313966503588,
    --       "text": "california",
    --       "start_index": 103,
    --       "end_index": 112,
    --       "result": {
    --         "name": "California",
    --         "value": "CA"
    --       }
    --     },
    --     {
    --       "type": "postal_code",
    --       "score": 0.9519646137063122,
    --       "text": "95128",
    --       "start_index": 149,
    --       "end_index": 153,
    --       "result": {
    --         "value": "95128"
    --       }
    --     }
    --   ]
    -- }

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

    DECLARE @v_type nvarchar(4000)

    DECLARE @text nvarchar(4000)

    DECLARE @start_index int

    DECLARE @end_index int

    DECLARE @Value nvarchar(4000)

    DECLARE @Line int

    DECLARE @v_Type nvarchar(4000)

    DECLARE @resultName nvarchar(4000)

    DECLARE @score nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @score OUT, 'score'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'address.name'
    DECLARE @Address_line1 nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Address_line1 OUT, 'address.address_line1'
    DECLARE @City_locality nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @City_locality OUT, 'address.city_locality'
    DECLARE @State_province nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @State_province OUT, 'address.state_province'
    DECLARE @Postal_code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Postal_code OUT, 'address.postal_code'
    DECLARE @Country_code nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Country_code OUT, 'address.country_code'
    DECLARE @Address_residential_indicator nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Address_residential_indicator OUT, 'address.address_residential_indicator'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'entities'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @v_type OUT, 'entities[i].type'
        EXEC sp_OAMethod @jResp, 'StringOf', @score OUT, 'entities[i].score'
        EXEC sp_OAMethod @jResp, 'StringOf', @text OUT, 'entities[i].text'
        EXEC sp_OAMethod @jResp, 'IntOf', @start_index OUT, 'entities[i].start_index'
        EXEC sp_OAMethod @jResp, 'IntOf', @end_index OUT, 'entities[i].end_index'
        EXEC sp_OAMethod @jResp, 'StringOf', @Value OUT, 'entities[i].result.value'
        EXEC sp_OAMethod @jResp, 'IntOf', @Line OUT, 'entities[i].result.line'
        EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'entities[i].result.type'
        EXEC sp_OAMethod @jResp, 'StringOf', @resultName OUT, 'entities[i].result.name'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

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 Miller’s house at 525 Winchester Blvd in San Jose (that\'s california, obviously). The zip code there is 95128.",
	"address": {
		"country_code": "US"
	}
}'
https://api.shipengine.com/v1/addresses/recognize

Postman Collection Item JSON

{
  "name": "Already-known fields",
  "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 Miller’s house at 525 Winchester Blvd in San Jose (that's california, obviously). The zip code there is 95128.\",\n\t\"address\": {\n\t\t\"country_code\": \"US\"\n\t}\n}"
    },
    "url": {
      "raw": "https://api.shipengine.com/v1/addresses/recognize",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "addresses",
        "recognize"
      ]
    },
    "description": "If you already know some parts of the address, you can provide them to ShipEngine, and we'll use those values. These values can also help us recognize other parts of the address."
  },
  "response": [
    {
      "name": "Already-known fields",
      "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 Miller’s house at 525 Winchester Blvd in San Jose (that's california, obviously). The zip code there is 95128.\",\n\t\"address\": {\n\t\t\"country_code\": \"US\"\n\t}\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:21:30 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.9710697187242877,\n    \"address\": {\n        \"name\": \"Amanda Miller\",\n        \"address_line1\": \"525 Winchester Blvd\",\n        \"city_locality\": \"San Jose\",\n        \"state_province\": \"CA\",\n        \"postal_code\": \"95128\",\n        \"country_code\": \"US\",\n        \"address_residential_indicator\": \"yes\"\n    },\n    \"entities\": [\n        {\n            \"type\": \"person\",\n            \"score\": 0.9519646137063122,\n            \"text\": \"Amanda Miller\",\n            \"start_index\": 38,\n            \"end_index\": 50,\n            \"result\": {\n                \"value\": \"Amanda Miller\"\n            }\n        },\n        {\n            \"type\": \"residential_indicator\",\n            \"score\": 0.9519646137063122,\n            \"text\": \"house\",\n            \"start_index\": 54,\n            \"end_index\": 58,\n            \"result\": {\n                \"value\": \"yes\"\n            }\n        },\n        {\n            \"type\": \"address_line\",\n            \"score\": 0.9805313966503588,\n            \"text\": \"525 Winchester Blvd\",\n            \"start_index\": 63,\n            \"end_index\": 81,\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\": 63,\n            \"end_index\": 65,\n            \"result\": {\n                \"type\": \"cardinal\",\n                \"value\": 525\n            }\n        },\n        {\n            \"type\": \"city_locality\",\n            \"score\": 0.9805313966503588,\n            \"text\": \"San Jose\",\n            \"start_index\": 86,\n            \"end_index\": 93,\n            \"result\": {\n                \"value\": \"San Jose\"\n            }\n        },\n        {\n            \"type\": \"state_province\",\n            \"score\": 0.9805313966503588,\n            \"text\": \"california\",\n            \"start_index\": 103,\n            \"end_index\": 112,\n            \"result\": {\n                \"name\": \"California\",\n                \"value\": \"CA\"\n            }\n        },\n        {\n            \"type\": \"postal_code\",\n            \"score\": 0.9519646137063122,\n            \"text\": \"95128\",\n            \"start_index\": 149,\n            \"end_index\": 153,\n            \"result\": {\n                \"value\": \"95128\"\n            }\n        }\n    ]\n}"
    }
  ]
}