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) UPS Rate RequestDemonstrates making a call to the UPS Rating Package web service. Note: This example uses a feature introduced in Chilkat v9.5.0.76.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, HttpResponse, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var success: Boolean; http: HCkHttp; url: PWideChar; json: HCkJsonObject; sb: HCkStringBuilder; resp: HCkHttpResponse; i: Integer; count_i: Integer; statusCode: PWideChar; statusDescription: PWideChar; customerContext: PWideChar; serviceCode: PWideChar; serviceDescription: PWideChar; billingWeightUnitOfMeasurementCode: PWideChar; billingWeightUnitOfMeasurementDescription: PWideChar; billingWeightWeight: PWideChar; transportationChargesCurrencyCode: PWideChar; transportationChargesMonetaryValue: PWideChar; serviceOptionsChargesCurrencyCode: PWideChar; serviceOptionsChargesMonetaryValue: PWideChar; totalChargesCurrencyCode: PWideChar; totalChargesMonetaryValue: PWideChar; ratedPackageTransportationChargesCurrencyCode: PWideChar; ratedPackageTransportationChargesMonetaryValue: PWideChar; ratedPackageServiceOptionsChargesCurrencyCode: PWideChar; ratedPackageServiceOptionsChargesMonetaryValue: PWideChar; ratedPackageTotalChargesCurrencyCode: PWideChar; ratedPackageTotalChargesMonetaryValue: PWideChar; ratedPackageWeight: PWideChar; ratedPackageBillingWeightUnitOfMeasurementCode: PWideChar; ratedPackageBillingWeightUnitOfMeasurementDescription: PWideChar; ratedPackageBillingWeightWeight: PWideChar; Code: PWideChar; Description: PWideChar; begin // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http := CkHttp_Create(); // This is the testing endpoint for the Rate Package JSON REST API: url := 'https://wwwcie.ups.com/rest/Rate'; // Send an HTTP POST request with the following JSON body: // { // "UPSSecurity": { // "UsernameToken": { // "Username": "UPS_USERNAME", // "Password": "UPS_PASSWORD" // }, // "ServiceAccessToken": { // "AccessLicenseNumber": "UPS_ACCESS_KEY" // } // }, // "RateRequest": { // "Request": { // "RequestOption": "Rate", // "TransactionReference": { // "CustomerContext": "Your Customer Context" // } // }, // "Shipment": { // "Shipper": { // "Name": "Apple Store, Michigan Ave", // "ShipperNumber": "1234", // "Address": { // "AddressLine": [ // "401 North Michigan Avenue" // ], // "City": "Chicago", // "StateProvinceCode": "IL", // "PostalCode": "60611", // "CountryCode": "US" // } // }, // "ShipTo": { // "Name": "Apple HQ", // "Address": { // "AddressLine": [ // "One Infinite Loop" // ], // "City": "Cupertino", // "StateProvinceCode": "CA", // "PostalCode": "95014", // "CountryCode": "US" // } // }, // "ShipFrom": { // "Name": "Apple Store, Michigan Ave", // "Address": { // "AddressLine": [ // "401 North Michigan Avenue" // ], // "City": "Chicago", // "StateProvinceCode": "IL", // "PostalCode": "60611", // "CountryCode": "US" // } // }, // "Service": { // "Code": "03", // "Description": "Service Code Description" // }, // "Package": { // "PackagingType": { // "Code": "02", // "Description": "Rate" // }, // "Dimensions": { // "UnitOfMeasurement": { // "Code": "IN", // "Description": "inches" // }, // "Length": "5", // "Width": "4", // "Height": "3" // }, // "PackageWeight": { // "UnitOfMeasurement": { // "Code": "Lbs", // "Description": "pounds" // }, // "Weight": "1" // } // }, // "ShipmentRatingOptions": { // "NegotiatedRatesIndicator": "" // } // } // } // } // Build the above JSON. // Note: The PathPrefix feature was added in Chilkat v9.5.0.76 json := CkJsonObject_Create(); CkJsonObject_UpdateString(json,'UPSSecurity.UsernameToken.Username','UPS_USERNAME'); CkJsonObject_UpdateString(json,'UPSSecurity.UsernameToken.Password','UPS_PASSWORD'); CkJsonObject_UpdateString(json,'UPSSecurity.ServiceAccessToken.AccessLicenseNumber','UPS_ACCESS_KEY'); CkJsonObject_UpdateString(json,'RateRequest.Request.RequestOption','Rate'); CkJsonObject_UpdateString(json,'RateRequest.Request.TransactionReference.CustomerContext','Your Customer Context'); CkJsonObject_putPathPrefix(json,'RateRequest.Shipment.Shipper.'); CkJsonObject_UpdateString(json,'Name','Apple Store, Michigan Ave'); CkJsonObject_UpdateString(json,'ShipperNumber','1234'); CkJsonObject_putPathPrefix(json,'RateRequest.Shipment.Shipper.Address.'); CkJsonObject_UpdateString(json,'AddressLine[0]','401 North Michigan Avenue'); CkJsonObject_UpdateString(json,'City','Chicago'); CkJsonObject_UpdateString(json,'StateProvinceCode','IL'); CkJsonObject_UpdateString(json,'PostalCode','60611'); CkJsonObject_UpdateString(json,'CountryCode','US'); CkJsonObject_putPathPrefix(json,''); CkJsonObject_UpdateString(json,'RateRequest.Shipment.ShipTo.Name','Apple HQ'); CkJsonObject_putPathPrefix(json,'RateRequest.Shipment.ShipTo.Address.'); CkJsonObject_UpdateString(json,'AddressLine[0]','One Infinite Loop'); CkJsonObject_UpdateString(json,'City','Cupertino'); CkJsonObject_UpdateString(json,'StateProvinceCode','CA'); CkJsonObject_UpdateString(json,'PostalCode','95014'); CkJsonObject_UpdateString(json,'CountryCode','US'); CkJsonObject_putPathPrefix(json,''); CkJsonObject_UpdateString(json,'RateRequest.Shipment.ShipFrom.Name','Apple Store, Michigan Ave'); CkJsonObject_putPathPrefix(json,'RateRequest.Shipment.ShipFrom.Address.'); CkJsonObject_UpdateString(json,'AddressLine[0]','401 North Michigan Avenue'); CkJsonObject_UpdateString(json,'City','Chicago'); CkJsonObject_UpdateString(json,'StateProvinceCode','IL'); CkJsonObject_UpdateString(json,'PostalCode','60611'); CkJsonObject_UpdateString(json,'CountryCode','US'); CkJsonObject_putPathPrefix(json,''); CkJsonObject_UpdateString(json,'RateRequest.Shipment.Service.Code','03'); CkJsonObject_UpdateString(json,'RateRequest.Shipment.Service.Description','Service Code Description'); CkJsonObject_putPathPrefix(json,'RateRequest.Shipment.Package.PackagingType.'); CkJsonObject_UpdateString(json,'Code','02'); CkJsonObject_UpdateString(json,'Description','Rate'); CkJsonObject_putPathPrefix(json,'RateRequest.Shipment.Package.Dimensions.'); CkJsonObject_UpdateString(json,'UnitOfMeasurement.Code','IN'); CkJsonObject_UpdateString(json,'UnitOfMeasurement.Description','inches'); CkJsonObject_UpdateString(json,'Length','5'); CkJsonObject_UpdateString(json,'Width','4'); CkJsonObject_UpdateString(json,'Height','3'); CkJsonObject_putPathPrefix(json,'RateRequest.Shipment.Package.PackageWeight.'); CkJsonObject_UpdateString(json,'UnitOfMeasurement.Code','Lbs'); CkJsonObject_UpdateString(json,'UnitOfMeasurement.Description','pounds'); CkJsonObject_UpdateString(json,'Weight','1'); CkJsonObject_putPathPrefix(json,''); CkJsonObject_UpdateString(json,'RateRequest.Shipment.ShipmentRatingOptions.NegotiatedRatesIndicator',''); CkJsonObject_putEmitCompact(json,False); Memo1.Lines.Add(CkJsonObject__emit(json)); sb := CkStringBuilder_Create(); resp := CkHttp_PostJson3(http,url,'application/json',json); if (CkHttp_getLastMethodSuccess(http) <> True) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; Memo1.Lines.Add('status = ' + IntToStr(CkHttpResponse_getStatusCode(resp))); // A 200 response status indicate success. if (CkHttpResponse_getStatusCode(resp) <> 200) then begin Memo1.Lines.Add(CkHttpResponse__bodyStr(resp)); Memo1.Lines.Add('Failed.'); CkHttpResponse_Dispose(resp); Exit; end; CkJsonObject_Load(json,CkHttpResponse__bodyStr(resp)); CkJsonObject_putEmitCompact(json,False); Memo1.Lines.Add(CkJsonObject__emit(json)); // Here is a sample response: // { // "RateResponse": { // "Response": { // "ResponseStatus": { // "Code": "1", // "Description": "Success" // }, // "Alert": [ // { // "Code": "110971", // "Description": "Your invoice may vary from the displayed reference rates" // }, // { // "Code": "119084", // "Description": "Missing / Invalid Shipper Number. Returned rates are Retail Rates." // }, // { // "Code": "120900", // "Description": "User Id and Shipper Number combination is not qualified to receive negotiated rates" // } // ], // "TransactionReference": { // "CustomerContext": "Your Customer Context" // } // }, // "RatedShipment": { // "Service": { // "Code": "03", // "Description": "" // }, // "RatedShipmentAlert": [ // { // "Code": "120900", // "Description": "User Id and Shipper Number combination is not qualified to receive negotiated rates." // }, // { // "Code": "110971", // "Description": "Your invoice may vary from the displayed reference rates" // }, // { // "Code": "119084", // "Description": "Missing / Invalid Shipper Number. Returned rates are Retail Rates." // } // ], // "BillingWeight": { // "UnitOfMeasurement": { // "Code": "LBS", // "Description": "Pounds" // }, // "Weight": "1.0" // }, // "TransportationCharges": { // "CurrencyCode": "USD", // "MonetaryValue": "11.04" // }, // "ServiceOptionsCharges": { // "CurrencyCode": "USD", // "MonetaryValue": "0.00" // }, // "TotalCharges": { // "CurrencyCode": "USD", // "MonetaryValue": "11.04" // }, // "RatedPackage": { // "TransportationCharges": { // "CurrencyCode": "USD", // "MonetaryValue": "11.04" // }, // "ServiceOptionsCharges": { // "CurrencyCode": "USD", // "MonetaryValue": "0.00" // }, // "TotalCharges": { // "CurrencyCode": "USD", // "MonetaryValue": "11.04" // }, // "Weight": "1.0", // "BillingWeight": { // "UnitOfMeasurement": { // "Code": "LBS", // "Description": "Pounds" // }, // "Weight": "1.0" // } // } // } // } // } // // Use the online tool at Generate JSON Parsing Code // to generate JSON parsing code. statusCode := CkJsonObject__stringOf(json,'RateResponse.Response.ResponseStatus.Code'); statusDescription := CkJsonObject__stringOf(json,'RateResponse.Response.ResponseStatus.Description'); customerContext := CkJsonObject__stringOf(json,'RateResponse.Response.TransactionReference.CustomerContext'); serviceCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.Service.Code'); serviceDescription := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.Service.Description'); billingWeightUnitOfMeasurementCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Code'); billingWeightUnitOfMeasurementDescription := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Description'); billingWeightWeight := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.BillingWeight.Weight'); transportationChargesCurrencyCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.TransportationCharges.CurrencyCode'); transportationChargesMonetaryValue := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.TransportationCharges.MonetaryValue'); serviceOptionsChargesCurrencyCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.ServiceOptionsCharges.CurrencyCode'); serviceOptionsChargesMonetaryValue := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.ServiceOptionsCharges.MonetaryValue'); totalChargesCurrencyCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.TotalCharges.CurrencyCode'); totalChargesMonetaryValue := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.TotalCharges.MonetaryValue'); ratedPackageTransportationChargesCurrencyCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.TransportationCharges.CurrencyCode'); ratedPackageTransportationChargesMonetaryValue := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.TransportationCharges.MonetaryValue'); ratedPackageServiceOptionsChargesCurrencyCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.CurrencyCode'); ratedPackageServiceOptionsChargesMonetaryValue := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.MonetaryValue'); ratedPackageTotalChargesCurrencyCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.TotalCharges.CurrencyCode'); ratedPackageTotalChargesMonetaryValue := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.TotalCharges.MonetaryValue'); ratedPackageWeight := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.Weight'); ratedPackageBillingWeightUnitOfMeasurementCode := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Code'); ratedPackageBillingWeightUnitOfMeasurementDescription := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Description'); ratedPackageBillingWeightWeight := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedPackage.BillingWeight.Weight'); i := 0; count_i := CkJsonObject_SizeOfArray(json,'RateResponse.Response.Alert'); while i < count_i do begin CkJsonObject_putI(json,i); Code := CkJsonObject__stringOf(json,'RateResponse.Response.Alert[i].Code'); Description := CkJsonObject__stringOf(json,'RateResponse.Response.Alert[i].Description'); i := i + 1; end; i := 0; count_i := CkJsonObject_SizeOfArray(json,'RateResponse.RatedShipment.RatedShipmentAlert'); while i < count_i do begin CkJsonObject_putI(json,i); Code := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedShipmentAlert[i].Code'); Description := CkJsonObject__stringOf(json,'RateResponse.RatedShipment.RatedShipmentAlert[i].Description'); i := i + 1; end; CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); end; |
© 2000-2023 Chilkat Software, Inc. All Rights Reserved.