Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Shippo Create Shipment ObjectCreate a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient address. For more information, see https://goshippo.com/docs/shipping-labels/
#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 *object_state; 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; int j; int count_j; const wchar_t *shipment; const wchar_t *amount; const wchar_t *currency; const wchar_t *amount_local; const wchar_t *currency_local; const wchar_t *provider; const wchar_t *provider_image_75; const wchar_t *provider_image_200; const wchar_t *servicelevelName; const wchar_t *servicelevelToken; const wchar_t *servicelevelTerms; int estimated_days; const wchar_t *arrives_by; const wchar_t *duration_terms; const wchar_t *carrier_account; const wchar_t *zone; const wchar_t *strVal; const wchar_t *source; const wchar_t *code; const wchar_t *text; 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_fromName; const wchar_t *address_fromCompany; const wchar_t *address_fromStreet_no; const wchar_t *address_fromStreet1; const wchar_t *address_fromStreet2; const wchar_t *address_fromStreet3; 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; BOOL address_fromTest; const wchar_t *address_toObject_id; BOOL address_toIs_complete; const wchar_t *address_toName; const wchar_t *address_toCompany; const wchar_t *address_toStreet_no; const wchar_t *address_toStreet1; const wchar_t *address_toStreet2; const wchar_t *address_toStreet3; 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; const wchar_t *address_toIs_residential; BOOL address_toTest; const wchar_t *shipment_date; const wchar_t *address_returnObject_id; BOOL address_returnIs_complete; const wchar_t *address_returnName; const wchar_t *address_returnCompany; const wchar_t *address_returnStreet_no; const wchar_t *address_returnStreet1; const wchar_t *address_returnStreet2; const wchar_t *address_returnStreet3; const wchar_t *address_returnCity; const wchar_t *address_returnState; const wchar_t *address_returnZip; const wchar_t *address_returnCountry; const wchar_t *address_returnPhone; const wchar_t *address_returnEmail; const wchar_t *address_returnIs_residential; BOOL address_returnTest; const wchar_t *alternate_address_to; const wchar_t *customs_declaration; const wchar_t *metadata; BOOL test; const wchar_t *order; 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(); // Implements the following CURL command: // curl https://api.goshippo.com/shipments/ \ // -H "Authorization: ShippoToken <API_TOKEN>" \ // -H "Content-Type: application/json" \ // -d '{ // "address_from":{ // "name":"Mr. Hippo", // "street1":"215 Clayton St.", // "city":"San Francisco", // "state":"CA", // "zip":"94117", // "country":"US" // }, // "address_to":{ // "name":"Mrs. Hippo", // "street1":"965 Mission St.", // "city":"San Francisco", // "state":"CA", // "zip":"94105", // "country":"US" // }, // "parcels":[{ // "length":"5", // "width":"5", // "height":"5", // "distance_unit":"in", // "weight":"2", // "mass_unit":"lb" // }], // "async": false // }' // Use this online tool to generate code from sample JSON: // Generate Code to Create JSON // The following JSON is sent in the request body. // { // "address_from": { // "name": "Mr. Hippo", // "street1": "215 Clayton St.", // "city": "San Francisco", // "state": "CA", // "zip": "94117", // "country": "US" // }, // "address_to": { // "name": "Mrs. Hippo", // "street1": "965 Mission St.", // "city": "San Francisco", // "state": "CA", // "zip": "94105", // "country": "US" // }, // "parcels": [ // { // "length": "5", // "width": "5", // "height": "5", // "distance_unit": "in", // "weight": "2", // "mass_unit": "lb" // } // ], // "async": false // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"address_from.name",L"Mr. Hippo"); CkJsonObjectW_UpdateString(json,L"address_from.street1",L"215 Clayton St."); CkJsonObjectW_UpdateString(json,L"address_from.city",L"San Francisco"); CkJsonObjectW_UpdateString(json,L"address_from.state",L"CA"); CkJsonObjectW_UpdateString(json,L"address_from.zip",L"94117"); CkJsonObjectW_UpdateString(json,L"address_from.country",L"US"); CkJsonObjectW_UpdateString(json,L"address_to.name",L"Mrs. Hippo"); CkJsonObjectW_UpdateString(json,L"address_to.street1",L"965 Mission St."); CkJsonObjectW_UpdateString(json,L"address_to.city",L"San Francisco"); CkJsonObjectW_UpdateString(json,L"address_to.state",L"CA"); CkJsonObjectW_UpdateString(json,L"address_to.zip",L"94105"); CkJsonObjectW_UpdateString(json,L"address_to.country",L"US"); CkJsonObjectW_UpdateString(json,L"parcels[0].length",L"5"); CkJsonObjectW_UpdateString(json,L"parcels[0].width",L"5"); CkJsonObjectW_UpdateString(json,L"parcels[0].height",L"5"); CkJsonObjectW_UpdateString(json,L"parcels[0].distance_unit",L"in"); CkJsonObjectW_UpdateString(json,L"parcels[0].weight",L"2"); CkJsonObjectW_UpdateString(json,L"parcels[0].mass_unit",L"lb"); CkJsonObjectW_UpdateBool(json,L"async",FALSE); CkHttpW_SetRequestHeader(http,L"Authorization",L"ShippoToken <API_TOKEN>"); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json"); resp = CkHttpW_PostJson3(http,L"https://api.goshippo.com/shipments/",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) // { // "carrier_accounts": [ // ], // "object_created": "2019-06-28T17:45:20.441Z", // "object_updated": "2019-06-28T17:45:20.511Z", // "object_id": "17b01795a4884d8a9002ccc17c9d09d1", // "object_owner": "admin@chilkatsoft.com", // "status": "SUCCESS", // "address_from": { // "object_id": "427489906b604f498f7bd2429ab7d9a1", // "is_complete": true, // "name": "Mr. Hippo", // "company": "", // "street_no": "", // "street1": "215 Clayton St.", // "validation_results": {}, // "street2": "", // "street3": "", // "city": "San Francisco", // "state": "CA", // "zip": "94117", // "country": "US", // "phone": "", // "email": "", // "is_residential": null, // "test": true // }, // "address_to": { // "object_id": "3d7166269a2844b7842ef9ace942ec7c", // "is_complete": true, // "name": "Mrs. Hippo", // "company": "", // "street_no": "", // "street1": "965 Mission St.", // "validation_results": {}, // "street2": "", // "street3": "", // "city": "San Francisco", // "state": "CA", // "zip": "94105", // "country": "US", // "phone": "", // "email": "", // "is_residential": null, // "test": true // }, // "parcels": [ // { // "object_state": "VALID", // "object_created": "2019-06-28T17:45:20.411Z", // "object_updated": "2019-06-28T17:45:20.455Z", // "object_id": "5af766ff15684a4186b0e3c833348fac", // "object_owner": "admin@chilkatsoft.com", // "template": null, // "extra": {}, // "length": "5.0000", // "width": "5.0000", // "height": "5.0000", // "distance_unit": "in", // "weight": "2.0000", // "mass_unit": "lb", // "value_amount": null, // "value_currency": null, // "metadata": "", // "line_items": [ // ], // "test": true // } // ], // "shipment_date": "2019-06-28T17:45:20.511Z", // "address_return": { // "object_id": "427489906b604f498f7bd2429ab7d9a1", // "is_complete": true, // "name": "Mr. Hippo", // "company": "", // "street_no": "", // "street1": "215 Clayton St.", // "validation_results": {}, // "street2": "", // "street3": "", // "city": "San Francisco", // "state": "CA", // "zip": "94117", // "country": "US", // "phone": "", // "email": "", // "is_residential": null, // "test": true // }, // "alternate_address_to": null, // "customs_declaration": null, // "extra": {}, // "rates": [ // { // "object_created": "2019-06-28T17:45:20.789Z", // "object_id": "dd7556c284e8444294d2ab7099e662d2", // "object_owner": "admin@chilkatsoft.com", // "shipment": "17b01795a4884d8a9002ccc17c9d09d1", // "attributes": [ // "FASTEST" // ], // "amount": "22.78", // "currency": "USD", // "amount_local": "22.78", // "currency_local": "USD", // "provider": "USPS", // "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png", // "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png", // "servicelevel": { // "name": "Priority Mail Express", // "token": "usps_priority_express", // "terms": "" // }, // "estimated_days": 2, // "arrives_by": null, // "duration_terms": "Overnight delivery to most U.S. locations.", // "messages": [ // ], // "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e", // "test": true, // "zone": "1" // }, // { // "object_created": "2019-06-28T17:45:20.786Z", // "object_id": "ec11cc297e2f4583986097f6d409e5c6", // "object_owner": "admin@chilkatsoft.com", // "shipment": "17b01795a4884d8a9002ccc17c9d09d1", // "attributes": [ // "BESTVALUE", // "CHEAPEST", // "FASTEST" // ], // "amount": "6.95", // "currency": "USD", // "amount_local": "6.95", // "currency_local": "USD", // "provider": "USPS", // "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png", // "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png", // "servicelevel": { // "name": "Priority Mail", // "token": "usps_priority", // "terms": "" // }, // "estimated_days": 2, // "arrives_by": null, // "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.", // "messages": [ // ], // "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e", // "test": true, // "zone": "1" // }, // { // "object_created": "2019-06-28T17:45:20.785Z", // "object_id": "d256586f539f4c93a2f02cc11f4942ac", // "object_owner": "admin@chilkatsoft.com", // "shipment": "17b01795a4884d8a9002ccc17c9d09d1", // "attributes": [ // ], // "amount": "7.32", // "currency": "USD", // "amount_local": "7.32", // "currency_local": "USD", // "provider": "USPS", // "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png", // "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png", // "servicelevel": { // "name": "Parcel Select", // "token": "usps_parcel_select", // "terms": "" // }, // "estimated_days": 7, // "arrives_by": null, // "duration_terms": "Delivery in 2 to 8 days.", // "messages": [ // ], // "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e", // "test": true, // "zone": "1" // } // ], // "messages": [ // { // "source": "DHLExpress", // "code": "", // "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US" // } // ], // "metadata": "", // "test": true, // "order": null // } // 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_fromName = CkJsonObjectW_stringOf(jResp,L"address_from.name"); address_fromCompany = CkJsonObjectW_stringOf(jResp,L"address_from.company"); address_fromStreet_no = CkJsonObjectW_stringOf(jResp,L"address_from.street_no"); address_fromStreet1 = CkJsonObjectW_stringOf(jResp,L"address_from.street1"); address_fromStreet2 = CkJsonObjectW_stringOf(jResp,L"address_from.street2"); address_fromStreet3 = CkJsonObjectW_stringOf(jResp,L"address_from.street3"); 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_fromTest = CkJsonObjectW_BoolOf(jResp,L"address_from.test"); 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_toCompany = CkJsonObjectW_stringOf(jResp,L"address_to.company"); address_toStreet_no = CkJsonObjectW_stringOf(jResp,L"address_to.street_no"); address_toStreet1 = CkJsonObjectW_stringOf(jResp,L"address_to.street1"); address_toStreet2 = CkJsonObjectW_stringOf(jResp,L"address_to.street2"); address_toStreet3 = CkJsonObjectW_stringOf(jResp,L"address_to.street3"); 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_stringOf(jResp,L"address_to.is_residential"); address_toTest = CkJsonObjectW_BoolOf(jResp,L"address_to.test"); shipment_date = CkJsonObjectW_stringOf(jResp,L"shipment_date"); address_returnObject_id = CkJsonObjectW_stringOf(jResp,L"address_return.object_id"); address_returnIs_complete = CkJsonObjectW_BoolOf(jResp,L"address_return.is_complete"); address_returnName = CkJsonObjectW_stringOf(jResp,L"address_return.name"); address_returnCompany = CkJsonObjectW_stringOf(jResp,L"address_return.company"); address_returnStreet_no = CkJsonObjectW_stringOf(jResp,L"address_return.street_no"); address_returnStreet1 = CkJsonObjectW_stringOf(jResp,L"address_return.street1"); address_returnStreet2 = CkJsonObjectW_stringOf(jResp,L"address_return.street2"); address_returnStreet3 = CkJsonObjectW_stringOf(jResp,L"address_return.street3"); address_returnCity = CkJsonObjectW_stringOf(jResp,L"address_return.city"); address_returnState = CkJsonObjectW_stringOf(jResp,L"address_return.state"); address_returnZip = CkJsonObjectW_stringOf(jResp,L"address_return.zip"); address_returnCountry = CkJsonObjectW_stringOf(jResp,L"address_return.country"); address_returnPhone = CkJsonObjectW_stringOf(jResp,L"address_return.phone"); address_returnEmail = CkJsonObjectW_stringOf(jResp,L"address_return.email"); address_returnIs_residential = CkJsonObjectW_stringOf(jResp,L"address_return.is_residential"); address_returnTest = CkJsonObjectW_BoolOf(jResp,L"address_return.test"); alternate_address_to = CkJsonObjectW_stringOf(jResp,L"alternate_address_to"); customs_declaration = CkJsonObjectW_stringOf(jResp,L"customs_declaration"); metadata = CkJsonObjectW_stringOf(jResp,L"metadata"); test = CkJsonObjectW_BoolOf(jResp,L"test"); order = CkJsonObjectW_stringOf(jResp,L"order"); 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"parcels"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); object_state = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_state"); object_created = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_created"); object_updated = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_updated"); object_id = CkJsonObjectW_stringOf(jResp,L"parcels[i].object_id"); 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); object_created = CkJsonObjectW_stringOf(jResp,L"rates[i].object_created"); object_id = CkJsonObjectW_stringOf(jResp,L"rates[i].object_id"); object_owner = CkJsonObjectW_stringOf(jResp,L"rates[i].object_owner"); shipment = CkJsonObjectW_stringOf(jResp,L"rates[i].shipment"); amount = CkJsonObjectW_stringOf(jResp,L"rates[i].amount"); currency = CkJsonObjectW_stringOf(jResp,L"rates[i].currency"); amount_local = CkJsonObjectW_stringOf(jResp,L"rates[i].amount_local"); currency_local = CkJsonObjectW_stringOf(jResp,L"rates[i].currency_local"); provider = CkJsonObjectW_stringOf(jResp,L"rates[i].provider"); provider_image_75 = CkJsonObjectW_stringOf(jResp,L"rates[i].provider_image_75"); provider_image_200 = CkJsonObjectW_stringOf(jResp,L"rates[i].provider_image_200"); servicelevelName = CkJsonObjectW_stringOf(jResp,L"rates[i].servicelevel.name"); servicelevelToken = CkJsonObjectW_stringOf(jResp,L"rates[i].servicelevel.token"); servicelevelTerms = CkJsonObjectW_stringOf(jResp,L"rates[i].servicelevel.terms"); estimated_days = CkJsonObjectW_IntOf(jResp,L"rates[i].estimated_days"); arrives_by = CkJsonObjectW_stringOf(jResp,L"rates[i].arrives_by"); duration_terms = CkJsonObjectW_stringOf(jResp,L"rates[i].duration_terms"); carrier_account = CkJsonObjectW_stringOf(jResp,L"rates[i].carrier_account"); test = CkJsonObjectW_BoolOf(jResp,L"rates[i].test"); zone = CkJsonObjectW_stringOf(jResp,L"rates[i].zone"); j = 0; count_j = CkJsonObjectW_SizeOfArray(jResp,L"rates[i].attributes"); while (j < count_j) { CkJsonObjectW_putJ(jResp,j); strVal = CkJsonObjectW_stringOf(jResp,L"rates[i].attributes[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"messages"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); source = CkJsonObjectW_stringOf(jResp,L"messages[i].source"); code = CkJsonObjectW_stringOf(jResp,L"messages[i].code"); text = CkJsonObjectW_stringOf(jResp,L"messages[i].text"); i = i + 1; } CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.