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 Adding MetadataDemonstrates how to add metadata to the tracking request through a POST request. For more information, see https://goshippo.com/docs/tracking/
#include <C_CkHttpW.h> #include <C_CkHttpRequestW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkHttpRequestW req; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; const wchar_t *object_created; const wchar_t *object_updated; const wchar_t *object_id; const wchar_t *status; const wchar_t *status_details; const wchar_t *status_date; const wchar_t *substatus; const wchar_t *locationCity; const wchar_t *locationState; const wchar_t *locationZip; const wchar_t *locationCountry; const wchar_t *carrier; const wchar_t *tracking_number; const wchar_t *address_fromCity; const wchar_t *address_fromState; const wchar_t *address_fromZip; const wchar_t *address_fromCountry; const wchar_t *address_toCity; const wchar_t *address_toState; const wchar_t *address_toZip; const wchar_t *address_toCountry; const wchar_t *eta; const wchar_t *original_eta; const wchar_t *servicelevelToken; const wchar_t *servicelevelName; const wchar_t *metadata; const wchar_t *tracking_statusObject_created; const wchar_t *tracking_statusObject_updated; const wchar_t *tracking_statusObject_id; const wchar_t *tracking_statusStatus; const wchar_t *tracking_statusStatus_details; const wchar_t *tracking_statusStatus_date; const wchar_t *tracking_statusSubstatus; const wchar_t *tracking_statusLocationCity; const wchar_t *tracking_statusLocationState; const wchar_t *tracking_statusLocationZip; const wchar_t *tracking_statusLocationCountry; const wchar_t *transaction; BOOL test; 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/tracks/ \ // -H "Authorization: ShippoToken <API_TOKEN>" \ // -d carrier="shippo" \ // -d tracking_number="SHIPPO_TRANSIT" \ // -d metadata="Order 000123" req = CkHttpRequestW_Create(); CkHttpRequestW_putHttpVerb(req,L"POST"); CkHttpRequestW_putPath(req,L"/tracks/"); CkHttpRequestW_putContentType(req,L"application/x-www-form-urlencoded"); CkHttpRequestW_AddParam(req,L"carrier",L"shippo"); CkHttpRequestW_AddParam(req,L"tracking_number",L"SHIPPO_TRANSIT"); CkHttpRequestW_AddParam(req,L"metadata",L"Order 000123"); CkHttpRequestW_AddHeader(req,L"Authorization",L"ShippoToken <API_TOKEN>"); resp = CkHttpW_PostUrlEncoded(http,L"https://api.goshippo.com/tracks/",req); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); 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); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // Sample JSON response: // (Sample code for parsing the JSON response is shown below) // { // "messages": [ // ], // "carrier": "shippo", // "tracking_number": "SHIPPO_TRANSIT", // "address_from": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // }, // "address_to": { // "city": "Chicago", // "state": "IL", // "zip": "60611", // "country": "US" // }, // "eta": "2019-07-07T17:07:44.989Z", // "original_eta": "2019-07-07T17:07:44.989Z", // "servicelevel": { // "token": "shippo_priority", // "name": "Priority Mail" // }, // "metadata": "Shippo test tracking", // "tracking_status": { // "object_created": "2019-07-04T17:07:45.003Z", // "object_updated": null, // "object_id": "ee35fb56f5d04021b36168abedc04573", // "status": "TRANSIT", // "status_details": "Your shipment has departed from the origin.", // "status_date": "2019-07-03T15:02:45.003Z", // "substatus": null, // "location": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // } // }, // "tracking_history": [ // { // "object_created": "2019-07-04T17:07:45.005Z", // "object_updated": null, // "object_id": "2121a59f53ed42e0ae0436f636179156", // "status": "UNKNOWN", // "status_details": "The carrier has received the electronic shipment information.", // "status_date": "2019-07-02T12:57:45.005Z", // "substatus": null, // "location": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // } // }, // { // "object_created": "2019-07-04T17:07:45.005Z", // "object_updated": null, // "object_id": "06f949db1a8245beaa28df264b368a76", // "status": "TRANSIT", // "status_details": "Your shipment has departed from the origin.", // "status_date": "2019-07-03T15:02:45.005Z", // "substatus": null, // "location": { // "city": "San Francisco", // "state": "CA", // "zip": "94103", // "country": "US" // } // } // ], // "transaction": null, // "test": true // } // 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. carrier = CkJsonObjectW_stringOf(jResp,L"carrier"); tracking_number = CkJsonObjectW_stringOf(jResp,L"tracking_number"); 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_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"); eta = CkJsonObjectW_stringOf(jResp,L"eta"); original_eta = CkJsonObjectW_stringOf(jResp,L"original_eta"); servicelevelToken = CkJsonObjectW_stringOf(jResp,L"servicelevel.token"); servicelevelName = CkJsonObjectW_stringOf(jResp,L"servicelevel.name"); metadata = CkJsonObjectW_stringOf(jResp,L"metadata"); tracking_statusObject_created = CkJsonObjectW_stringOf(jResp,L"tracking_status.object_created"); tracking_statusObject_updated = CkJsonObjectW_stringOf(jResp,L"tracking_status.object_updated"); tracking_statusObject_id = CkJsonObjectW_stringOf(jResp,L"tracking_status.object_id"); tracking_statusStatus = CkJsonObjectW_stringOf(jResp,L"tracking_status.status"); tracking_statusStatus_details = CkJsonObjectW_stringOf(jResp,L"tracking_status.status_details"); tracking_statusStatus_date = CkJsonObjectW_stringOf(jResp,L"tracking_status.status_date"); tracking_statusSubstatus = CkJsonObjectW_stringOf(jResp,L"tracking_status.substatus"); tracking_statusLocationCity = CkJsonObjectW_stringOf(jResp,L"tracking_status.location.city"); tracking_statusLocationState = CkJsonObjectW_stringOf(jResp,L"tracking_status.location.state"); tracking_statusLocationZip = CkJsonObjectW_stringOf(jResp,L"tracking_status.location.zip"); tracking_statusLocationCountry = CkJsonObjectW_stringOf(jResp,L"tracking_status.location.country"); transaction = CkJsonObjectW_stringOf(jResp,L"transaction"); test = CkJsonObjectW_BoolOf(jResp,L"test"); i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"messages"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(jResp,L"tracking_history"); while (i < count_i) { CkJsonObjectW_putI(jResp,i); object_created = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].object_created"); object_updated = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].object_updated"); object_id = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].object_id"); status = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].status"); status_details = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].status_details"); status_date = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].status_date"); substatus = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].substatus"); locationCity = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].location.city"); locationState = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].location.state"); locationZip = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].location.zip"); locationCountry = CkJsonObjectW_stringOf(jResp,L"tracking_history[i].location.country"); i = i + 1; } CkHttpW_Dispose(http); CkHttpRequestW_Dispose(req); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.