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) 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 <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkStringBuilderW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { BOOL success; HCkHttpW http; const wchar_t *url; HCkJsonObjectW json; HCkStringBuilderW sb; HCkHttpResponseW resp; int i; int count_i; const wchar_t *statusCode; const wchar_t *statusDescription; const wchar_t *customerContext; const wchar_t *serviceCode; const wchar_t *serviceDescription; const wchar_t *billingWeightUnitOfMeasurementCode; const wchar_t *billingWeightUnitOfMeasurementDescription; const wchar_t *billingWeightWeight; const wchar_t *transportationChargesCurrencyCode; const wchar_t *transportationChargesMonetaryValue; const wchar_t *serviceOptionsChargesCurrencyCode; const wchar_t *serviceOptionsChargesMonetaryValue; const wchar_t *totalChargesCurrencyCode; const wchar_t *totalChargesMonetaryValue; const wchar_t *ratedPackageTransportationChargesCurrencyCode; const wchar_t *ratedPackageTransportationChargesMonetaryValue; const wchar_t *ratedPackageServiceOptionsChargesCurrencyCode; const wchar_t *ratedPackageServiceOptionsChargesMonetaryValue; const wchar_t *ratedPackageTotalChargesCurrencyCode; const wchar_t *ratedPackageTotalChargesMonetaryValue; const wchar_t *ratedPackageWeight; const wchar_t *ratedPackageBillingWeightUnitOfMeasurementCode; const wchar_t *ratedPackageBillingWeightUnitOfMeasurementDescription; const wchar_t *ratedPackageBillingWeightWeight; const wchar_t *Code; const wchar_t *Description; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // This is the testing endpoint for the Rate Package JSON REST API: url = L"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 = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"UPSSecurity.UsernameToken.Username",L"UPS_USERNAME"); CkJsonObjectW_UpdateString(json,L"UPSSecurity.UsernameToken.Password",L"UPS_PASSWORD"); CkJsonObjectW_UpdateString(json,L"UPSSecurity.ServiceAccessToken.AccessLicenseNumber",L"UPS_ACCESS_KEY"); CkJsonObjectW_UpdateString(json,L"RateRequest.Request.RequestOption",L"Rate"); CkJsonObjectW_UpdateString(json,L"RateRequest.Request.TransactionReference.CustomerContext",L"Your Customer Context"); CkJsonObjectW_putPathPrefix(json,L"RateRequest.Shipment.Shipper."); CkJsonObjectW_UpdateString(json,L"Name",L"Apple Store, Michigan Ave"); CkJsonObjectW_UpdateString(json,L"ShipperNumber",L"1234"); CkJsonObjectW_putPathPrefix(json,L"RateRequest.Shipment.Shipper.Address."); CkJsonObjectW_UpdateString(json,L"AddressLine[0]",L"401 North Michigan Avenue"); CkJsonObjectW_UpdateString(json,L"City",L"Chicago"); CkJsonObjectW_UpdateString(json,L"StateProvinceCode",L"IL"); CkJsonObjectW_UpdateString(json,L"PostalCode",L"60611"); CkJsonObjectW_UpdateString(json,L"CountryCode",L"US"); CkJsonObjectW_putPathPrefix(json,L""); CkJsonObjectW_UpdateString(json,L"RateRequest.Shipment.ShipTo.Name",L"Apple HQ"); CkJsonObjectW_putPathPrefix(json,L"RateRequest.Shipment.ShipTo.Address."); CkJsonObjectW_UpdateString(json,L"AddressLine[0]",L"One Infinite Loop"); CkJsonObjectW_UpdateString(json,L"City",L"Cupertino"); CkJsonObjectW_UpdateString(json,L"StateProvinceCode",L"CA"); CkJsonObjectW_UpdateString(json,L"PostalCode",L"95014"); CkJsonObjectW_UpdateString(json,L"CountryCode",L"US"); CkJsonObjectW_putPathPrefix(json,L""); CkJsonObjectW_UpdateString(json,L"RateRequest.Shipment.ShipFrom.Name",L"Apple Store, Michigan Ave"); CkJsonObjectW_putPathPrefix(json,L"RateRequest.Shipment.ShipFrom.Address."); CkJsonObjectW_UpdateString(json,L"AddressLine[0]",L"401 North Michigan Avenue"); CkJsonObjectW_UpdateString(json,L"City",L"Chicago"); CkJsonObjectW_UpdateString(json,L"StateProvinceCode",L"IL"); CkJsonObjectW_UpdateString(json,L"PostalCode",L"60611"); CkJsonObjectW_UpdateString(json,L"CountryCode",L"US"); CkJsonObjectW_putPathPrefix(json,L""); CkJsonObjectW_UpdateString(json,L"RateRequest.Shipment.Service.Code",L"03"); CkJsonObjectW_UpdateString(json,L"RateRequest.Shipment.Service.Description",L"Service Code Description"); CkJsonObjectW_putPathPrefix(json,L"RateRequest.Shipment.Package.PackagingType."); CkJsonObjectW_UpdateString(json,L"Code",L"02"); CkJsonObjectW_UpdateString(json,L"Description",L"Rate"); CkJsonObjectW_putPathPrefix(json,L"RateRequest.Shipment.Package.Dimensions."); CkJsonObjectW_UpdateString(json,L"UnitOfMeasurement.Code",L"IN"); CkJsonObjectW_UpdateString(json,L"UnitOfMeasurement.Description",L"inches"); CkJsonObjectW_UpdateString(json,L"Length",L"5"); CkJsonObjectW_UpdateString(json,L"Width",L"4"); CkJsonObjectW_UpdateString(json,L"Height",L"3"); CkJsonObjectW_putPathPrefix(json,L"RateRequest.Shipment.Package.PackageWeight."); CkJsonObjectW_UpdateString(json,L"UnitOfMeasurement.Code",L"Lbs"); CkJsonObjectW_UpdateString(json,L"UnitOfMeasurement.Description",L"pounds"); CkJsonObjectW_UpdateString(json,L"Weight",L"1"); CkJsonObjectW_putPathPrefix(json,L""); CkJsonObjectW_UpdateString(json,L"RateRequest.Shipment.ShipmentRatingOptions.NegotiatedRatesIndicator",L""); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); sb = CkStringBuilderW_Create(); resp = CkHttpW_PostJson3(http,url,L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); return; } wprintf(L"status = %d\n",CkHttpResponseW_getStatusCode(resp)); // A 200 response status indicate success. if (CkHttpResponseW_getStatusCode(resp) != 200) { wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); return; } CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp)); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_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 = CkJsonObjectW_stringOf(json,L"RateResponse.Response.ResponseStatus.Code"); statusDescription = CkJsonObjectW_stringOf(json,L"RateResponse.Response.ResponseStatus.Description"); customerContext = CkJsonObjectW_stringOf(json,L"RateResponse.Response.TransactionReference.CustomerContext"); serviceCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.Service.Code"); serviceDescription = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.Service.Description"); billingWeightUnitOfMeasurementCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Code"); billingWeightUnitOfMeasurementDescription = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Description"); billingWeightWeight = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.BillingWeight.Weight"); transportationChargesCurrencyCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.TransportationCharges.CurrencyCode"); transportationChargesMonetaryValue = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.TransportationCharges.MonetaryValue"); serviceOptionsChargesCurrencyCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.ServiceOptionsCharges.CurrencyCode"); serviceOptionsChargesMonetaryValue = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.ServiceOptionsCharges.MonetaryValue"); totalChargesCurrencyCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.TotalCharges.CurrencyCode"); totalChargesMonetaryValue = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.TotalCharges.MonetaryValue"); ratedPackageTransportationChargesCurrencyCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.TransportationCharges.CurrencyCode"); ratedPackageTransportationChargesMonetaryValue = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.TransportationCharges.MonetaryValue"); ratedPackageServiceOptionsChargesCurrencyCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.CurrencyCode"); ratedPackageServiceOptionsChargesMonetaryValue = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.MonetaryValue"); ratedPackageTotalChargesCurrencyCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.TotalCharges.CurrencyCode"); ratedPackageTotalChargesMonetaryValue = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.TotalCharges.MonetaryValue"); ratedPackageWeight = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.Weight"); ratedPackageBillingWeightUnitOfMeasurementCode = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Code"); ratedPackageBillingWeightUnitOfMeasurementDescription = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Description"); ratedPackageBillingWeightWeight = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedPackage.BillingWeight.Weight"); i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"RateResponse.Response.Alert"); while (i < count_i) { CkJsonObjectW_putI(json,i); Code = CkJsonObjectW_stringOf(json,L"RateResponse.Response.Alert[i].Code"); Description = CkJsonObjectW_stringOf(json,L"RateResponse.Response.Alert[i].Description"); i = i + 1; } i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"RateResponse.RatedShipment.RatedShipmentAlert"); while (i < count_i) { CkJsonObjectW_putI(json,i); Code = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedShipmentAlert[i].Code"); Description = CkJsonObjectW_stringOf(json,L"RateResponse.RatedShipment.RatedShipmentAlert[i].Description"); i = i + 1; } CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); } |
© 2000-2023 Chilkat Software, Inc. All Rights Reserved.