Back to Collection Items
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
HCkHttp http;
BOOL success;
HCkJsonObject json;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *v_type;
const char *text;
int start_index;
int end_index;
const char *Value;
int Line;
const char *v_Type;
const char *resultName;
const char *score;
const char *Name;
const char *Address_line1;
const char *City_locality;
const char *State_province;
const char *Postal_code;
const char *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 = CkHttp_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 Miller’s house at 525 Winchester Blvd in San Jose (that's california, obviously). The zip code there is 95128."
// }
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"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.");
CkHttp_SetRequestHeader(http,"API-Key","{{API_KEY}}");
CkHttp_SetRequestHeader(http,"Content-Type","application/json");
resp = CkHttp_PostJson3(http,"https://api.shipengine.com/v1/addresses/recognize","application/json",json);
if (CkHttp_getLastMethodSuccess(http) == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttpResponse_Dispose(resp);
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
return;
}
CkHttpResponse_Dispose(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",
// "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
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
score = CkJsonObject_stringOf(jResp,"score");
Name = CkJsonObject_stringOf(jResp,"address.name");
Address_line1 = CkJsonObject_stringOf(jResp,"address.address_line1");
City_locality = CkJsonObject_stringOf(jResp,"address.city_locality");
State_province = CkJsonObject_stringOf(jResp,"address.state_province");
Postal_code = CkJsonObject_stringOf(jResp,"address.postal_code");
Address_residential_indicator = CkJsonObject_stringOf(jResp,"address.address_residential_indicator");
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"entities");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
v_type = CkJsonObject_stringOf(jResp,"entities[i].type");
score = CkJsonObject_stringOf(jResp,"entities[i].score");
text = CkJsonObject_stringOf(jResp,"entities[i].text");
start_index = CkJsonObject_IntOf(jResp,"entities[i].start_index");
end_index = CkJsonObject_IntOf(jResp,"entities[i].end_index");
Value = CkJsonObject_stringOf(jResp,"entities[i].result.value");
Line = CkJsonObject_IntOf(jResp,"entities[i].result.line");
v_Type = CkJsonObject_stringOf(jResp,"entities[i].result.type");
resultName = CkJsonObject_stringOf(jResp,"entities[i].result.name");
i = i + 1;
}
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_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 Miller’s house at 525 Winchester Blvd in San Jose (that\'s california, obviously). The zip code there is 95128."
}'
https://api.shipengine.com/v1/addresses/recognize
Postman Collection Item JSON
{
"name": "Parse an Address",
"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}"
},
"url": {
"raw": "https://api.shipengine.com/v1/addresses/recognize",
"protocol": "https",
"host": [
"api",
"shipengine",
"com"
],
"path": [
"v1",
"addresses",
"recognize"
]
},
"description": "This request shows how to pass unstructured text to ShipEngine that contains address information. ShipEngine will recognize any address-related parts of the text and give you back an `address` object."
},
"response": [
{
"name": "Parse an Address",
"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}",
"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 14:59:19 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 \"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}"
}
]
}