Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) 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.
#include <CkHttp.h> #include <CkJsonObject.h> #include <CkStringBuilder.h> #include <CkHttpResponse.h> void ChilkatSample(void) { CkString strOut; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. bool success; CkHttp http; // This is the testing endpoint for the Rate Package JSON REST API: const char *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 CkJsonObject json; json.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME"); json.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD"); json.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY"); json.UpdateString("RateRequest.Request.RequestOption","Rate"); json.UpdateString("RateRequest.Request.TransactionReference.CustomerContext","Your Customer Context"); json.put_PathPrefix("RateRequest.Shipment.Shipper."); json.UpdateString("Name","Apple Store, Michigan Ave"); json.UpdateString("ShipperNumber","1234"); json.put_PathPrefix("RateRequest.Shipment.Shipper.Address."); json.UpdateString("AddressLine[0]","401 North Michigan Avenue"); json.UpdateString("City","Chicago"); json.UpdateString("StateProvinceCode","IL"); json.UpdateString("PostalCode","60611"); json.UpdateString("CountryCode","US"); json.put_PathPrefix(""); json.UpdateString("RateRequest.Shipment.ShipTo.Name","Apple HQ"); json.put_PathPrefix("RateRequest.Shipment.ShipTo.Address."); json.UpdateString("AddressLine[0]","One Infinite Loop"); json.UpdateString("City","Cupertino"); json.UpdateString("StateProvinceCode","CA"); json.UpdateString("PostalCode","95014"); json.UpdateString("CountryCode","US"); json.put_PathPrefix(""); json.UpdateString("RateRequest.Shipment.ShipFrom.Name","Apple Store, Michigan Ave"); json.put_PathPrefix("RateRequest.Shipment.ShipFrom.Address."); json.UpdateString("AddressLine[0]","401 North Michigan Avenue"); json.UpdateString("City","Chicago"); json.UpdateString("StateProvinceCode","IL"); json.UpdateString("PostalCode","60611"); json.UpdateString("CountryCode","US"); json.put_PathPrefix(""); json.UpdateString("RateRequest.Shipment.Service.Code","03"); json.UpdateString("RateRequest.Shipment.Service.Description","Service Code Description"); json.put_PathPrefix("RateRequest.Shipment.Package.PackagingType."); json.UpdateString("Code","02"); json.UpdateString("Description","Rate"); json.put_PathPrefix("RateRequest.Shipment.Package.Dimensions."); json.UpdateString("UnitOfMeasurement.Code","IN"); json.UpdateString("UnitOfMeasurement.Description","inches"); json.UpdateString("Length","5"); json.UpdateString("Width","4"); json.UpdateString("Height","3"); json.put_PathPrefix("RateRequest.Shipment.Package.PackageWeight."); json.UpdateString("UnitOfMeasurement.Code","Lbs"); json.UpdateString("UnitOfMeasurement.Description","pounds"); json.UpdateString("Weight","1"); json.put_PathPrefix(""); json.UpdateString("RateRequest.Shipment.ShipmentRatingOptions.NegotiatedRatesIndicator",""); json.put_EmitCompact(false); strOut.append(json.emit()); strOut.append("\r\n"); CkStringBuilder sb; CkHttpResponse *resp = http.PostJson3(url,"application/json",json); if (http.get_LastMethodSuccess() != true) { strOut.append(http.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } strOut.append("status = "); strOut.appendInt(resp->get_StatusCode()); strOut.append("\r\n"); // A 200 response status indicate success. if (resp->get_StatusCode() != 200) { strOut.append(resp->bodyStr()); strOut.append("\r\n"); strOut.append("Failed."); strOut.append("\r\n"); delete resp; SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } json.Load(resp->bodyStr()); json.put_EmitCompact(false); strOut.append(json.emit()); strOut.append("\r\n"); // 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. int i; int count_i; const char *statusCode = json.stringOf("RateResponse.Response.ResponseStatus.Code"); const char *statusDescription = json.stringOf("RateResponse.Response.ResponseStatus.Description"); const char *customerContext = json.stringOf("RateResponse.Response.TransactionReference.CustomerContext"); const char *serviceCode = json.stringOf("RateResponse.RatedShipment.Service.Code"); const char *serviceDescription = json.stringOf("RateResponse.RatedShipment.Service.Description"); const char *billingWeightUnitOfMeasurementCode = json.stringOf("RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Code"); const char *billingWeightUnitOfMeasurementDescription = json.stringOf("RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Description"); const char *billingWeightWeight = json.stringOf("RateResponse.RatedShipment.BillingWeight.Weight"); const char *transportationChargesCurrencyCode = json.stringOf("RateResponse.RatedShipment.TransportationCharges.CurrencyCode"); const char *transportationChargesMonetaryValue = json.stringOf("RateResponse.RatedShipment.TransportationCharges.MonetaryValue"); const char *serviceOptionsChargesCurrencyCode = json.stringOf("RateResponse.RatedShipment.ServiceOptionsCharges.CurrencyCode"); const char *serviceOptionsChargesMonetaryValue = json.stringOf("RateResponse.RatedShipment.ServiceOptionsCharges.MonetaryValue"); const char *totalChargesCurrencyCode = json.stringOf("RateResponse.RatedShipment.TotalCharges.CurrencyCode"); const char *totalChargesMonetaryValue = json.stringOf("RateResponse.RatedShipment.TotalCharges.MonetaryValue"); const char *ratedPackageTransportationChargesCurrencyCode = json.stringOf("RateResponse.RatedShipment.RatedPackage.TransportationCharges.CurrencyCode"); const char *ratedPackageTransportationChargesMonetaryValue = json.stringOf("RateResponse.RatedShipment.RatedPackage.TransportationCharges.MonetaryValue"); const char *ratedPackageServiceOptionsChargesCurrencyCode = json.stringOf("RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.CurrencyCode"); const char *ratedPackageServiceOptionsChargesMonetaryValue = json.stringOf("RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.MonetaryValue"); const char *ratedPackageTotalChargesCurrencyCode = json.stringOf("RateResponse.RatedShipment.RatedPackage.TotalCharges.CurrencyCode"); const char *ratedPackageTotalChargesMonetaryValue = json.stringOf("RateResponse.RatedShipment.RatedPackage.TotalCharges.MonetaryValue"); const char *ratedPackageWeight = json.stringOf("RateResponse.RatedShipment.RatedPackage.Weight"); const char *ratedPackageBillingWeightUnitOfMeasurementCode = json.stringOf("RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Code"); const char *ratedPackageBillingWeightUnitOfMeasurementDescription = json.stringOf("RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Description"); const char *ratedPackageBillingWeightWeight = json.stringOf("RateResponse.RatedShipment.RatedPackage.BillingWeight.Weight"); const char *Code = 0; const char *Description = 0; i = 0; count_i = json.SizeOfArray("RateResponse.Response.Alert"); while (i < count_i) { json.put_I(i); Code = json.stringOf("RateResponse.Response.Alert[i].Code"); Description = json.stringOf("RateResponse.Response.Alert[i].Description"); i = i + 1; } i = 0; count_i = json.SizeOfArray("RateResponse.RatedShipment.RatedShipmentAlert"); while (i < count_i) { json.put_I(i); Code = json.stringOf("RateResponse.RatedShipment.RatedShipmentAlert[i].Code"); Description = json.stringOf("RateResponse.RatedShipment.RatedShipmentAlert[i].Description"); i = i + 1; } SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.