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
(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_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 *object_state; const char *template; const char *length; const char *width; const char *height; const char *distance_unit; const char *weight; const char *mass_unit; const char *value_amount; const char *value_currency; int j; int count_j; const char *shipment; const char *amount; const char *currency; const char *amount_local; const char *currency_local; const char *provider; const char *provider_image_75; const char *provider_image_200; const char *servicelevelName; const char *servicelevelToken; const char *servicelevelTerms; int estimated_days; const char *arrives_by; const char *duration_terms; const char *carrier_account; const char *zone; const char *strVal; const char *source; const char *code; const char *text; const char *object_created; const char *object_updated; const char *object_id; const char *object_owner; const char *status; const char *address_fromObject_id; BOOL address_fromIs_complete; const char *address_fromName; const char *address_fromCompany; const char *address_fromStreet_no; const char *address_fromStreet1; const char *address_fromStreet2; const char *address_fromStreet3; const char *address_fromCity; const char *address_fromState; const char *address_fromZip; const char *address_fromCountry; const char *address_fromPhone; const char *address_fromEmail; const char *address_fromIs_residential; BOOL address_fromTest; const char *address_toObject_id; BOOL address_toIs_complete; const char *address_toName; const char *address_toCompany; const char *address_toStreet_no; const char *address_toStreet1; const char *address_toStreet2; const char *address_toStreet3; const char *address_toCity; const char *address_toState; const char *address_toZip; const char *address_toCountry; const char *address_toPhone; const char *address_toEmail; const char *address_toIs_residential; BOOL address_toTest; const char *shipment_date; const char *address_returnObject_id; BOOL address_returnIs_complete; const char *address_returnName; const char *address_returnCompany; const char *address_returnStreet_no; const char *address_returnStreet1; const char *address_returnStreet2; const char *address_returnStreet3; const char *address_returnCity; const char *address_returnState; const char *address_returnZip; const char *address_returnCountry; const char *address_returnPhone; const char *address_returnEmail; const char *address_returnIs_residential; BOOL address_returnTest; const char *alternate_address_to; const char *customs_declaration; const char *metadata; BOOL test; const char *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 = CkHttp_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 = CkJsonObject_Create(); CkJsonObject_UpdateString(json,"address_from.name","Mr. Hippo"); CkJsonObject_UpdateString(json,"address_from.street1","215 Clayton St."); CkJsonObject_UpdateString(json,"address_from.city","San Francisco"); CkJsonObject_UpdateString(json,"address_from.state","CA"); CkJsonObject_UpdateString(json,"address_from.zip","94117"); CkJsonObject_UpdateString(json,"address_from.country","US"); CkJsonObject_UpdateString(json,"address_to.name","Mrs. Hippo"); CkJsonObject_UpdateString(json,"address_to.street1","965 Mission St."); CkJsonObject_UpdateString(json,"address_to.city","San Francisco"); CkJsonObject_UpdateString(json,"address_to.state","CA"); CkJsonObject_UpdateString(json,"address_to.zip","94105"); CkJsonObject_UpdateString(json,"address_to.country","US"); CkJsonObject_UpdateString(json,"parcels[0].length","5"); CkJsonObject_UpdateString(json,"parcels[0].width","5"); CkJsonObject_UpdateString(json,"parcels[0].height","5"); CkJsonObject_UpdateString(json,"parcels[0].distance_unit","in"); CkJsonObject_UpdateString(json,"parcels[0].weight","2"); CkJsonObject_UpdateString(json,"parcels[0].mass_unit","lb"); CkJsonObject_UpdateBool(json,"async",FALSE); CkHttp_SetRequestHeader(http,"Authorization","ShippoToken <API_TOKEN>"); CkHttp_SetRequestHeader(http,"Content-Type","application/json"); resp = CkHttp_PostJson3(http,"https://api.goshippo.com/shipments/","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) // { // "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 = CkJsonObject_stringOf(jResp,"object_created"); object_updated = CkJsonObject_stringOf(jResp,"object_updated"); object_id = CkJsonObject_stringOf(jResp,"object_id"); object_owner = CkJsonObject_stringOf(jResp,"object_owner"); status = CkJsonObject_stringOf(jResp,"status"); address_fromObject_id = CkJsonObject_stringOf(jResp,"address_from.object_id"); address_fromIs_complete = CkJsonObject_BoolOf(jResp,"address_from.is_complete"); address_fromName = CkJsonObject_stringOf(jResp,"address_from.name"); address_fromCompany = CkJsonObject_stringOf(jResp,"address_from.company"); address_fromStreet_no = CkJsonObject_stringOf(jResp,"address_from.street_no"); address_fromStreet1 = CkJsonObject_stringOf(jResp,"address_from.street1"); address_fromStreet2 = CkJsonObject_stringOf(jResp,"address_from.street2"); address_fromStreet3 = CkJsonObject_stringOf(jResp,"address_from.street3"); address_fromCity = CkJsonObject_stringOf(jResp,"address_from.city"); address_fromState = CkJsonObject_stringOf(jResp,"address_from.state"); address_fromZip = CkJsonObject_stringOf(jResp,"address_from.zip"); address_fromCountry = CkJsonObject_stringOf(jResp,"address_from.country"); address_fromPhone = CkJsonObject_stringOf(jResp,"address_from.phone"); address_fromEmail = CkJsonObject_stringOf(jResp,"address_from.email"); address_fromIs_residential = CkJsonObject_stringOf(jResp,"address_from.is_residential"); address_fromTest = CkJsonObject_BoolOf(jResp,"address_from.test"); address_toObject_id = CkJsonObject_stringOf(jResp,"address_to.object_id"); address_toIs_complete = CkJsonObject_BoolOf(jResp,"address_to.is_complete"); address_toName = CkJsonObject_stringOf(jResp,"address_to.name"); address_toCompany = CkJsonObject_stringOf(jResp,"address_to.company"); address_toStreet_no = CkJsonObject_stringOf(jResp,"address_to.street_no"); address_toStreet1 = CkJsonObject_stringOf(jResp,"address_to.street1"); address_toStreet2 = CkJsonObject_stringOf(jResp,"address_to.street2"); address_toStreet3 = CkJsonObject_stringOf(jResp,"address_to.street3"); address_toCity = CkJsonObject_stringOf(jResp,"address_to.city"); address_toState = CkJsonObject_stringOf(jResp,"address_to.state"); address_toZip = CkJsonObject_stringOf(jResp,"address_to.zip"); address_toCountry = CkJsonObject_stringOf(jResp,"address_to.country"); address_toPhone = CkJsonObject_stringOf(jResp,"address_to.phone"); address_toEmail = CkJsonObject_stringOf(jResp,"address_to.email"); address_toIs_residential = CkJsonObject_stringOf(jResp,"address_to.is_residential"); address_toTest = CkJsonObject_BoolOf(jResp,"address_to.test"); shipment_date = CkJsonObject_stringOf(jResp,"shipment_date"); address_returnObject_id = CkJsonObject_stringOf(jResp,"address_return.object_id"); address_returnIs_complete = CkJsonObject_BoolOf(jResp,"address_return.is_complete"); address_returnName = CkJsonObject_stringOf(jResp,"address_return.name"); address_returnCompany = CkJsonObject_stringOf(jResp,"address_return.company"); address_returnStreet_no = CkJsonObject_stringOf(jResp,"address_return.street_no"); address_returnStreet1 = CkJsonObject_stringOf(jResp,"address_return.street1"); address_returnStreet2 = CkJsonObject_stringOf(jResp,"address_return.street2"); address_returnStreet3 = CkJsonObject_stringOf(jResp,"address_return.street3"); address_returnCity = CkJsonObject_stringOf(jResp,"address_return.city"); address_returnState = CkJsonObject_stringOf(jResp,"address_return.state"); address_returnZip = CkJsonObject_stringOf(jResp,"address_return.zip"); address_returnCountry = CkJsonObject_stringOf(jResp,"address_return.country"); address_returnPhone = CkJsonObject_stringOf(jResp,"address_return.phone"); address_returnEmail = CkJsonObject_stringOf(jResp,"address_return.email"); address_returnIs_residential = CkJsonObject_stringOf(jResp,"address_return.is_residential"); address_returnTest = CkJsonObject_BoolOf(jResp,"address_return.test"); alternate_address_to = CkJsonObject_stringOf(jResp,"alternate_address_to"); customs_declaration = CkJsonObject_stringOf(jResp,"customs_declaration"); metadata = CkJsonObject_stringOf(jResp,"metadata"); test = CkJsonObject_BoolOf(jResp,"test"); order = CkJsonObject_stringOf(jResp,"order"); i = 0; count_i = CkJsonObject_SizeOfArray(jResp,"carrier_accounts"); while (i < count_i) { CkJsonObject_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObject_SizeOfArray(jResp,"parcels"); while (i < count_i) { CkJsonObject_putI(jResp,i); object_state = CkJsonObject_stringOf(jResp,"parcels[i].object_state"); object_created = CkJsonObject_stringOf(jResp,"parcels[i].object_created"); object_updated = CkJsonObject_stringOf(jResp,"parcels[i].object_updated"); object_id = CkJsonObject_stringOf(jResp,"parcels[i].object_id"); object_owner = CkJsonObject_stringOf(jResp,"parcels[i].object_owner"); template = CkJsonObject_stringOf(jResp,"parcels[i].template"); length = CkJsonObject_stringOf(jResp,"parcels[i].length"); width = CkJsonObject_stringOf(jResp,"parcels[i].width"); height = CkJsonObject_stringOf(jResp,"parcels[i].height"); distance_unit = CkJsonObject_stringOf(jResp,"parcels[i].distance_unit"); weight = CkJsonObject_stringOf(jResp,"parcels[i].weight"); mass_unit = CkJsonObject_stringOf(jResp,"parcels[i].mass_unit"); value_amount = CkJsonObject_stringOf(jResp,"parcels[i].value_amount"); value_currency = CkJsonObject_stringOf(jResp,"parcels[i].value_currency"); metadata = CkJsonObject_stringOf(jResp,"parcels[i].metadata"); test = CkJsonObject_BoolOf(jResp,"parcels[i].test"); j = 0; count_j = CkJsonObject_SizeOfArray(jResp,"parcels[i].line_items"); while (j < count_j) { CkJsonObject_putJ(jResp,j); j = j + 1; } i = i + 1; } i = 0; count_i = CkJsonObject_SizeOfArray(jResp,"rates"); while (i < count_i) { CkJsonObject_putI(jResp,i); object_created = CkJsonObject_stringOf(jResp,"rates[i].object_created"); object_id = CkJsonObject_stringOf(jResp,"rates[i].object_id"); object_owner = CkJsonObject_stringOf(jResp,"rates[i].object_owner"); shipment = CkJsonObject_stringOf(jResp,"rates[i].shipment"); amount = CkJsonObject_stringOf(jResp,"rates[i].amount"); currency = CkJsonObject_stringOf(jResp,"rates[i].currency"); amount_local = CkJsonObject_stringOf(jResp,"rates[i].amount_local"); currency_local = CkJsonObject_stringOf(jResp,"rates[i].currency_local"); provider = CkJsonObject_stringOf(jResp,"rates[i].provider"); provider_image_75 = CkJsonObject_stringOf(jResp,"rates[i].provider_image_75"); provider_image_200 = CkJsonObject_stringOf(jResp,"rates[i].provider_image_200"); servicelevelName = CkJsonObject_stringOf(jResp,"rates[i].servicelevel.name"); servicelevelToken = CkJsonObject_stringOf(jResp,"rates[i].servicelevel.token"); servicelevelTerms = CkJsonObject_stringOf(jResp,"rates[i].servicelevel.terms"); estimated_days = CkJsonObject_IntOf(jResp,"rates[i].estimated_days"); arrives_by = CkJsonObject_stringOf(jResp,"rates[i].arrives_by"); duration_terms = CkJsonObject_stringOf(jResp,"rates[i].duration_terms"); carrier_account = CkJsonObject_stringOf(jResp,"rates[i].carrier_account"); test = CkJsonObject_BoolOf(jResp,"rates[i].test"); zone = CkJsonObject_stringOf(jResp,"rates[i].zone"); j = 0; count_j = CkJsonObject_SizeOfArray(jResp,"rates[i].attributes"); while (j < count_j) { CkJsonObject_putJ(jResp,j); strVal = CkJsonObject_stringOf(jResp,"rates[i].attributes[j]"); j = j + 1; } j = 0; count_j = CkJsonObject_SizeOfArray(jResp,"rates[i].messages"); while (j < count_j) { CkJsonObject_putJ(jResp,j); j = j + 1; } i = i + 1; } i = 0; count_i = CkJsonObject_SizeOfArray(jResp,"messages"); while (i < count_i) { CkJsonObject_putI(jResp,i); source = CkJsonObject_stringOf(jResp,"messages[i].source"); code = CkJsonObject_stringOf(jResp,"messages[i].code"); text = CkJsonObject_stringOf(jResp,"messages[i].text"); i = i + 1; } CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.