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
(Delphi DLL) Shippo Adding MetadataDemonstrates how to add metadata to the tracking request through a POST request. For more information, see https://goshippo.com/docs/tracking/
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, HttpRequest, HttpResponse, StringBuilder, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var http: HCkHttp; success: Boolean; req: HCkHttpRequest; resp: HCkHttpResponse; sbResponseBody: HCkStringBuilder; jResp: HCkJsonObject; respStatusCode: Integer; object_created: PWideChar; object_updated: PWideChar; object_id: PWideChar; status: PWideChar; status_details: PWideChar; status_date: PWideChar; substatus: PWideChar; locationCity: PWideChar; locationState: PWideChar; locationZip: PWideChar; locationCountry: PWideChar; carrier: PWideChar; tracking_number: PWideChar; address_fromCity: PWideChar; address_fromState: PWideChar; address_fromZip: PWideChar; address_fromCountry: PWideChar; address_toCity: PWideChar; address_toState: PWideChar; address_toZip: PWideChar; address_toCountry: PWideChar; eta: PWideChar; original_eta: PWideChar; servicelevelToken: PWideChar; servicelevelName: PWideChar; metadata: PWideChar; tracking_statusObject_created: PWideChar; tracking_statusObject_updated: PWideChar; tracking_statusObject_id: PWideChar; tracking_statusStatus: PWideChar; tracking_statusStatus_details: PWideChar; tracking_statusStatus_date: PWideChar; tracking_statusSubstatus: PWideChar; tracking_statusLocationCity: PWideChar; tracking_statusLocationState: PWideChar; tracking_statusLocationZip: PWideChar; tracking_statusLocationCountry: PWideChar; transaction: PWideChar; test: Boolean; i: Integer; count_i: Integer; begin // 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/tracks/ \ // -H "Authorization: ShippoToken <API_TOKEN>" \ // -d carrier="shippo" \ // -d tracking_number="SHIPPO_TRANSIT" \ // -d metadata="Order 000123" req := CkHttpRequest_Create(); CkHttpRequest_putHttpVerb(req,'POST'); CkHttpRequest_putPath(req,'/tracks/'); CkHttpRequest_putContentType(req,'application/x-www-form-urlencoded'); CkHttpRequest_AddParam(req,'carrier','shippo'); CkHttpRequest_AddParam(req,'tracking_number','SHIPPO_TRANSIT'); CkHttpRequest_AddParam(req,'metadata','Order 000123'); CkHttpRequest_AddHeader(req,'Authorization','ShippoToken <API_TOKEN>'); resp := CkHttp_PostUrlEncoded(http,'https://api.goshippo.com/tracks/',req); if (CkHttp_getLastMethodSuccess(http) = False) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; sbResponseBody := CkStringBuilder_Create(); CkHttpResponse_GetBodySb(resp,sbResponseBody); jResp := CkJsonObject_Create(); CkJsonObject_LoadSb(jResp,sbResponseBody); CkJsonObject_putEmitCompact(jResp,False); Memo1.Lines.Add('Response Body:'); Memo1.Lines.Add(CkJsonObject__emit(jResp)); respStatusCode := CkHttpResponse_getStatusCode(resp); Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode)); if (respStatusCode >= 400) then begin Memo1.Lines.Add('Response Header:'); Memo1.Lines.Add(CkHttpResponse__header(resp)); Memo1.Lines.Add('Failed.'); CkHttpResponse_Dispose(resp); Exit; end; CkHttpResponse_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 carrier := CkJsonObject__stringOf(jResp,'carrier'); tracking_number := CkJsonObject__stringOf(jResp,'tracking_number'); 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_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'); eta := CkJsonObject__stringOf(jResp,'eta'); original_eta := CkJsonObject__stringOf(jResp,'original_eta'); servicelevelToken := CkJsonObject__stringOf(jResp,'servicelevel.token'); servicelevelName := CkJsonObject__stringOf(jResp,'servicelevel.name'); metadata := CkJsonObject__stringOf(jResp,'metadata'); tracking_statusObject_created := CkJsonObject__stringOf(jResp,'tracking_status.object_created'); tracking_statusObject_updated := CkJsonObject__stringOf(jResp,'tracking_status.object_updated'); tracking_statusObject_id := CkJsonObject__stringOf(jResp,'tracking_status.object_id'); tracking_statusStatus := CkJsonObject__stringOf(jResp,'tracking_status.status'); tracking_statusStatus_details := CkJsonObject__stringOf(jResp,'tracking_status.status_details'); tracking_statusStatus_date := CkJsonObject__stringOf(jResp,'tracking_status.status_date'); tracking_statusSubstatus := CkJsonObject__stringOf(jResp,'tracking_status.substatus'); tracking_statusLocationCity := CkJsonObject__stringOf(jResp,'tracking_status.location.city'); tracking_statusLocationState := CkJsonObject__stringOf(jResp,'tracking_status.location.state'); tracking_statusLocationZip := CkJsonObject__stringOf(jResp,'tracking_status.location.zip'); tracking_statusLocationCountry := CkJsonObject__stringOf(jResp,'tracking_status.location.country'); transaction := CkJsonObject__stringOf(jResp,'transaction'); test := CkJsonObject_BoolOf(jResp,'test'); i := 0; count_i := CkJsonObject_SizeOfArray(jResp,'messages'); while i < count_i do begin CkJsonObject_putI(jResp,i); i := i + 1; end; i := 0; count_i := CkJsonObject_SizeOfArray(jResp,'tracking_history'); while i < count_i do begin CkJsonObject_putI(jResp,i); object_created := CkJsonObject__stringOf(jResp,'tracking_history[i].object_created'); object_updated := CkJsonObject__stringOf(jResp,'tracking_history[i].object_updated'); object_id := CkJsonObject__stringOf(jResp,'tracking_history[i].object_id'); status := CkJsonObject__stringOf(jResp,'tracking_history[i].status'); status_details := CkJsonObject__stringOf(jResp,'tracking_history[i].status_details'); status_date := CkJsonObject__stringOf(jResp,'tracking_history[i].status_date'); substatus := CkJsonObject__stringOf(jResp,'tracking_history[i].substatus'); locationCity := CkJsonObject__stringOf(jResp,'tracking_history[i].location.city'); locationState := CkJsonObject__stringOf(jResp,'tracking_history[i].location.state'); locationZip := CkJsonObject__stringOf(jResp,'tracking_history[i].location.zip'); locationCountry := CkJsonObject__stringOf(jResp,'tracking_history[i].location.country'); i := i + 1; end; CkHttp_Dispose(http); CkHttpRequest_Dispose(req); CkStringBuilder_Dispose(sbResponseBody); CkJsonObject_Dispose(jResp); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.