![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Shippo Create the Shipment, Get Rates, and Purchase LabelDemonstrates how retrieve rates and create labels for international shipments. Note: This example requires Chilkat v11.0.0 or greater. For more information, see https://goshippo.com/docs/international/
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { BOOL success; HCkHttpW http; HCkJsonObjectW json; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *template; const wchar_t *length; const wchar_t *width; const wchar_t *height; const wchar_t *distance_unit; const wchar_t *weight; const wchar_t *mass_unit; const wchar_t *value_amount; const wchar_t *value_currency; BOOL test; int j; int count_j; int intVal; const wchar_t *object_created; const wchar_t *object_updated; const wchar_t *object_id; const wchar_t *object_owner; const wchar_t *status; const wchar_t *address_fromObject_id; BOOL address_fromIs_complete; const wchar_t *address_fromCompany; const wchar_t *address_fromStreet_no; const wchar_t *address_fromName; const wchar_t *address_fromStreet1; const wchar_t *address_fromStreet2; const wchar_t *address_fromCity; const wchar_t *address_fromState; const wchar_t *address_fromZip; const wchar_t *address_fromCountry; const wchar_t *address_fromPhone; const wchar_t *address_fromEmail; const wchar_t *address_fromIs_residential; const wchar_t *address_toObject_id; BOOL address_toIs_complete; const wchar_t *address_toName; const wchar_t *address_toStreet1; const wchar_t *address_toCity; const wchar_t *address_toState; const wchar_t *address_toZip; const wchar_t *address_toCountry; const wchar_t *address_toPhone; const wchar_t *address_toEmail; BOOL address_toIs_residential; const wchar_t *address_return; const wchar_t *shipment_date; const wchar_t *extraInsuranceCurrency; const wchar_t *extraInsuranceAmount; const wchar_t *extraReference_1; const wchar_t *extraReference_2; const wchar_t *customs_declaration; const wchar_t *metadata; int i; int count_i; success = FALSE; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // Implements the following CURL command: // curl https://api.goshippo.com/customs/declarations/ \ // -H "Authorization: ShippoToken <API_TOKEN>" \ // -H "Content-Type: application/json" \ // -d '{ // "contents_type": "MERCHANDISE", // "non_delivery_option": "RETURN", // "certify": true, // "certify_signer": "Simon Kreuz", // "incoterm": "DDU", // "items": [{ // "description": "T-shirt", // "quantity": 20, // "net_weight": "5", // "mass_unit": "lb", // "value_amount": "200", // "value_currency": "USD", // "tariff_number": "", // "origin_country": "US" // }] // }' // Use this online tool to generate code from sample JSON: // Generate Code to Create JSON // The following JSON is sent in the request body. // { // "contents_type": "MERCHANDISE", // "non_delivery_option": "RETURN", // "certify": true, // "certify_signer": "Simon Kreuz", // "incoterm": "DDU", // "items": [ // { // "description": "T-shirt", // "quantity": 20, // "net_weight": "5", // "mass_unit": "lb", // "value_amount": "200", // "value_currency": "USD", // "tariff_number": "", // "origin_country": "US" // } // ] // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"contents_type",L"MERCHANDISE"); CkJsonObjectW_UpdateString(json,L"non_delivery_option",L"RETURN"); CkJsonObjectW_UpdateBool(json,L"certify",TRUE); CkJsonObjectW_UpdateString(json,L"certify_signer",L"Simon Kreuz"); CkJsonObjectW_UpdateString(json,L"incoterm",L"DDU"); CkJsonObjectW_UpdateString(json,L"items[0].description",L"T-shirt"); CkJsonObjectW_UpdateInt(json,L"items[0].quantity",20); CkJsonObjectW_UpdateString(json,L"items[0].net_weight",L"5"); CkJsonObjectW_UpdateString(json,L"items[0].mass_unit",L"lb"); CkJsonObjectW_UpdateString(json,L"items[0].value_amount",L"200"); CkJsonObjectW_UpdateString(json,L"items[0].value_currency",L"USD"); CkJsonObjectW_UpdateString(json,L"items[0].tariff_number",L""); CkJsonObjectW_UpdateString(json,L"items[0].origin_country",L"US"); CkHttpW_SetRequestHeader(http,L"Authorization",L"ShippoToken <API_TOKEN>"); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json"); resp = CkHttpResponseW_Create(); success = CkHttpW_HttpJson(http,L"POST",L"https://api.goshippo.com/customs/declarations/",json,L"application/json",resp); if (success == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkHttpResponseW_Dispose(resp); 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"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkHttpResponseW_Dispose(resp); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } // Sample JSON response: // (Sample code for parsing the JSON response is shown below) // { // "object_created": "2014-07-17T00:04:06.163Z", // "object_updated": "2014-07-17T00:04:06.163Z", // "object_id": "89436997a794439ab47999701e60392e", // "object_owner": "shippotle@goshippo.com", // "status": "SUCCESS", // "address_from": { // "object_id": "0943ae4e373e4120a99c337e496dcce8", // "validation_results": {}, // "is_complete": true, // "company": "", // "street_no": "", // "name": "Mr. Hippo", // "street1": "215 Clayton St.", // "street2": "", // "city": "San Francisco", // "state": "CA", // "zip": "94117", // "country": "US", // "phone": "+15553419393", // "email": "support@goshippo.com", // "is_residential": null // }, // "address_to": { // "object_id": "4c7185d353764d0985a6a7825aed8ffb", // "validation_results": {}, // "is_complete": true, // "name": "Mrs. Hippo", // "street1": "200 University Ave W", // "city": "Waterloo", // "state": "ON", // "zip": "N2L 3G1", // "country": "CA", // "phone": "+1 555 341 9393", // "email": "support@goshippo.com", // "is_residential": false // }, // "address_return": null, // "parcels": [ // { // "object_id": "ec952343dd4843c39b42aca620471fd5", // "object_created": "2013-12-01T06:24:21.121Z", // "object_updated": "2013-12-01T06:24:21.121Z", // "object_owner": "shippotle@goshippo.com", // "template": null, // "length": "5", // "width": "5", // "height": "5", // "distance_unit": "in", // "weight": "2", // "mass_unit": "lb", // "value_amount": null, // "value_currency": null, // "metadata": "", // "line_items": [ // ], // "test": true // } // ], // "shipment_date": "2013-12-03T12:00:00Z", // "extra": { // "insurance": { // "currency": "", // "amount": "" // }, // "reference_1": "", // "reference_2": "" // }, // "customs_declaration": "b741b99f95e841639b54272834bc478c", // "rates": [ // { // "object_created": "2014-07-17T00:04:06.263Z", // "object_id": "545ab0a1a6ea4c9f9adb2512a57d6d8b", // "object_owner": "shippotle@goshippo.com", // "shipment": "89436997a794439ab47999701e60392e", // "attributes": [ // ], // "amount": "5.50", // "currency": "USD", // "amount_local": "5.50", // "currency_local": "USD", // "provider": "USPS", // "provider_image_75": "https://cdn2.goshippo.com/providers/75/USPS.png", // "provider_image_200": "https://cdn2.goshippo.com/providers/200/USPS.png", // "servicelevel": { // "name": "Priority Mail", // "token": "usps_priority", // "terms": "" // }, // "days": 2, // "arrives_by": null, // "duration_terms": "Delivery in 1 to 3 business days.", // "messages": [ // ], // "carrier_account": "078870331023437cb917f5187429b093", // "test": false, // "zone": 1 // }, // ... // ], // "carrier_accounts": [ // ], // "messages": [ // ], // "metadata": "Customer ID 123456" // } // Sample code for parsing the JSON response... // Use the following online tool to generate parsing code from sample JSON: // Generate Parsing Code from JSON // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat. // See this example explaining how this memory should be used: const char * functions. object_created = CkJsonObjectW_stringOf(jResp,L"object_created"); object_updated = CkJsonObjectW_stringOf(jResp,L"object_updated"); object_id = CkJsonObjectW_stringOf(jResp,L"object_id"); object_owner = CkJsonObjectW_stringOf(jResp,L"object_owner"); status = CkJsonObjectW_stringOf(jResp,L"status"); address_fromObject_id = CkJsonObjectW_stringOf(jResp,L"address_from.object_id"); address_fromIs_complete = CkJsonObjectW_BoolOf(jResp,L"address_from.is_complete"); address_fromCompany = CkJsonObjectW_stringOf(jResp,L"address_from.company"); address_fromStreet_no = CkJsonObjectW_stringOf(jResp,L"address_from.street_no"); address_fromName = CkJsonObjectW_stringOf(jResp,L"address_from.name"); address_fromStreet1 = CkJsonObjectW_stringOf(jResp,L"address_from.street1"); address_fromStreet2 = CkJsonObjectW_stringOf(jResp,L"address_from.street2"); address_fromCity = CkJsonObjectW_stringOf(jResp,L"address_from.city"); address_fromState = CkJsonObjectW_stringOf(jResp,L"address_from.state"); address_fromZip = CkJsonObjectW_stringOf(jResp,L"address_from.zip"); address_fromCountry = CkJsonObjectW_stringOf(jResp,L"address_from.country"); address_fromPhone = CkJsonObjectW_stringOf(jResp,L"address_from.phone"); address_fromEmail = CkJsonObjectW_stringOf(jResp,L"address_from.email"); address_fromIs_residential = CkJsonObjectW_stringOf(jResp,L"address_from.is_residential"); address_toObject_id = CkJsonObjectW_stringOf(jResp,L"address_to.object_id"); address_toIs_complete = CkJsonObjectW_BoolOf(jResp,L"address_to.is_complete"); address_toName = CkJsonObjectW_stringOf(jResp,L"address_to.name"); address_toStreet1 = CkJsonObjectW_stringOf(jResp,L"address_to.street1"); address_toCity = CkJsonObjectW_stringOf(jResp,L"address_to.city"); address_toState = CkJsonObjectW_stringOf(jResp,L"address_to.state"); address_toZip = CkJsonObjectW_stringOf(jResp,L"address_to.zip"); address_toCountry = CkJsonObjectW_stringOf(jResp,L"address_to.country"); address_toPhone = CkJsonObjectW_stringOf(jResp,L"address_to.phone"); address_toEmail = CkJsonObjectW_stringOf(jResp,L"address_to.email"); address_toIs_residential = CkJsonObjectW_BoolOf(jResp,L"address_to.is_residential"); address_return = CkJsonObjectW_stringOf(jResp,L"address_return"); shipment_date = CkJsonObjectW_stringOf(jResp,L"shipment_date"); extraInsuranceCurrency = CkJsonObjectW_stringOf(jResp,L"extra.insurance.currency"); extraInsuranceAmount = CkJsonObjectW_stringOf(jResp,L"extra.insurance.amount"); extraReference_1 = CkJsonObjectW_stringOf(jResp,L"extra.reference_1"); extraReference_2 = CkJsonObjectW_stringOf(jResp,L"extra.reference_2"); customs_declaration = CkJsonObjectW_stringOf(jResp,L"customs_declaration"); metadata = CkJsonObjectW_stringOf(jResp,L"metadata"); i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"parcels"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); object_id = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_id"); object_created = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_created"); object_updated = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_updated"); object_owner = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_owner"); template = CkJsonObjectW_stringOf(jResp,L"parcels[i].template"); length = CkJsonObjectW_stringOf(jResp,L"parcels[i].length"); width = CkJsonObjectW_stringOf(jResp,L"parcels[i].width"); height = CkJsonObjectW_stringOf(jResp,L"parcels[i].height"); distance_unit = CkJsonObjectW_stringOf(jResp,L"parcels[i].distance_unit"); weight = CkJsonObjectW_stringOf(jResp,L"parcels[i].weight"); mass_unit = CkJsonObjectW_stringOf(jResp,L"parcels[i].mass_unit"); value_amount = CkJsonObjectW_stringOf(jResp,L"parcels[i].value_amount"); value_currency = CkJsonObjectW_stringOf(jResp,L"parcels[i].value_currency"); metadata = CkJsonObjectW_stringOf(jResp,L"parcels[i].metadata"); test = CkJsonObjectW_BoolOf(jResp,L"parcels[i].test"); j = 0; count_j = CkJsonObjectW_SizeOfArray(jResp,L"parcels[i].line_items"); while (j < count_j) { CkJsonObjectW_putJ(jResp,j); j = j + 1; } i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"rates"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); intVal = CkJsonObjectW_IntOf(jResp,L"rates[i]"); j = 0; count_j = CkJsonObjectW_SizeOfArray(jResp,L"rates[i].attributes"); while (j < count_j) { CkJsonObjectW_putJ(jResp,j); j = j + 1; } j = 0; count_j = CkJsonObjectW_SizeOfArray(jResp,L"rates[i].messages"); while (j < count_j) { CkJsonObjectW_putJ(jResp,j); j = j + 1; } i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"carrier_accounts"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"messages"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkHttpResponseW_Dispose(resp); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.