Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Unicode C++ Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Unicode C++) UPS Rate Request

Demonstrates making a call to the UPS Rating Package web service.

Note: This example uses a feature introduced in Chilkat v9.5.0.76.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    bool success;

    CkHttpW http;

    // This is the testing endpoint for the Rate Package JSON REST API:
    const wchar_t *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
    CkJsonObjectW json;
    json.UpdateString(L"UPSSecurity.UsernameToken.Username",L"UPS_USERNAME");
    json.UpdateString(L"UPSSecurity.UsernameToken.Password",L"UPS_PASSWORD");
    json.UpdateString(L"UPSSecurity.ServiceAccessToken.AccessLicenseNumber",L"UPS_ACCESS_KEY");
    json.UpdateString(L"RateRequest.Request.RequestOption",L"Rate");
    json.UpdateString(L"RateRequest.Request.TransactionReference.CustomerContext",L"Your Customer Context");

    json.put_PathPrefix(L"RateRequest.Shipment.Shipper.");
    json.UpdateString(L"Name",L"Apple Store, Michigan Ave");
    json.UpdateString(L"ShipperNumber",L"1234");

    json.put_PathPrefix(L"RateRequest.Shipment.Shipper.Address.");
    json.UpdateString(L"AddressLine[0]",L"401 North Michigan Avenue");
    json.UpdateString(L"City",L"Chicago");
    json.UpdateString(L"StateProvinceCode",L"IL");
    json.UpdateString(L"PostalCode",L"60611");
    json.UpdateString(L"CountryCode",L"US");

    json.put_PathPrefix(L"");
    json.UpdateString(L"RateRequest.Shipment.ShipTo.Name",L"Apple HQ");

    json.put_PathPrefix(L"RateRequest.Shipment.ShipTo.Address.");
    json.UpdateString(L"AddressLine[0]",L"One Infinite Loop");
    json.UpdateString(L"City",L"Cupertino");
    json.UpdateString(L"StateProvinceCode",L"CA");
    json.UpdateString(L"PostalCode",L"95014");
    json.UpdateString(L"CountryCode",L"US");

    json.put_PathPrefix(L"");
    json.UpdateString(L"RateRequest.Shipment.ShipFrom.Name",L"Apple Store, Michigan Ave");

    json.put_PathPrefix(L"RateRequest.Shipment.ShipFrom.Address.");
    json.UpdateString(L"AddressLine[0]",L"401 North Michigan Avenue");
    json.UpdateString(L"City",L"Chicago");
    json.UpdateString(L"StateProvinceCode",L"IL");
    json.UpdateString(L"PostalCode",L"60611");
    json.UpdateString(L"CountryCode",L"US");

    json.put_PathPrefix(L"");
    json.UpdateString(L"RateRequest.Shipment.Service.Code",L"03");
    json.UpdateString(L"RateRequest.Shipment.Service.Description",L"Service Code Description");

    json.put_PathPrefix(L"RateRequest.Shipment.Package.PackagingType.");
    json.UpdateString(L"Code",L"02");
    json.UpdateString(L"Description",L"Rate");

    json.put_PathPrefix(L"RateRequest.Shipment.Package.Dimensions.");
    json.UpdateString(L"UnitOfMeasurement.Code",L"IN");
    json.UpdateString(L"UnitOfMeasurement.Description",L"inches");
    json.UpdateString(L"Length",L"5");
    json.UpdateString(L"Width",L"4");
    json.UpdateString(L"Height",L"3");

    json.put_PathPrefix(L"RateRequest.Shipment.Package.PackageWeight.");
    json.UpdateString(L"UnitOfMeasurement.Code",L"Lbs");
    json.UpdateString(L"UnitOfMeasurement.Description",L"pounds");
    json.UpdateString(L"Weight",L"1");

    json.put_PathPrefix(L"");
    json.UpdateString(L"RateRequest.Shipment.ShipmentRatingOptions.NegotiatedRatesIndicator",L"");

    json.put_EmitCompact(false);
    wprintf(L"%s\n",json.emit());

    CkStringBuilderW sb;
    CkHttpResponseW *resp = http.PostJson3(url,L"application/json",json);
    if (http.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"status = %d\n",resp->get_StatusCode());

    // A 200 response status indicate success.
    if (resp->get_StatusCode() != 200) {
        wprintf(L"%s\n",resp->bodyStr());
        wprintf(L"Failed.\n");
        delete resp;
        return;
    }

    json.Load(resp->bodyStr());
    json.put_EmitCompact(false);
    wprintf(L"%s\n",json.emit());

    // 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 wchar_t *statusCode = json.stringOf(L"RateResponse.Response.ResponseStatus.Code");
    const wchar_t *statusDescription = json.stringOf(L"RateResponse.Response.ResponseStatus.Description");
    const wchar_t *customerContext = json.stringOf(L"RateResponse.Response.TransactionReference.CustomerContext");
    const wchar_t *serviceCode = json.stringOf(L"RateResponse.RatedShipment.Service.Code");
    const wchar_t *serviceDescription = json.stringOf(L"RateResponse.RatedShipment.Service.Description");
    const wchar_t *billingWeightUnitOfMeasurementCode = json.stringOf(L"RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Code");
    const wchar_t *billingWeightUnitOfMeasurementDescription = json.stringOf(L"RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Description");
    const wchar_t *billingWeightWeight = json.stringOf(L"RateResponse.RatedShipment.BillingWeight.Weight");
    const wchar_t *transportationChargesCurrencyCode = json.stringOf(L"RateResponse.RatedShipment.TransportationCharges.CurrencyCode");
    const wchar_t *transportationChargesMonetaryValue = json.stringOf(L"RateResponse.RatedShipment.TransportationCharges.MonetaryValue");
    const wchar_t *serviceOptionsChargesCurrencyCode = json.stringOf(L"RateResponse.RatedShipment.ServiceOptionsCharges.CurrencyCode");
    const wchar_t *serviceOptionsChargesMonetaryValue = json.stringOf(L"RateResponse.RatedShipment.ServiceOptionsCharges.MonetaryValue");
    const wchar_t *totalChargesCurrencyCode = json.stringOf(L"RateResponse.RatedShipment.TotalCharges.CurrencyCode");
    const wchar_t *totalChargesMonetaryValue = json.stringOf(L"RateResponse.RatedShipment.TotalCharges.MonetaryValue");
    const wchar_t *ratedPackageTransportationChargesCurrencyCode = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.TransportationCharges.CurrencyCode");
    const wchar_t *ratedPackageTransportationChargesMonetaryValue = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.TransportationCharges.MonetaryValue");
    const wchar_t *ratedPackageServiceOptionsChargesCurrencyCode = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.CurrencyCode");
    const wchar_t *ratedPackageServiceOptionsChargesMonetaryValue = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.MonetaryValue");
    const wchar_t *ratedPackageTotalChargesCurrencyCode = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.TotalCharges.CurrencyCode");
    const wchar_t *ratedPackageTotalChargesMonetaryValue = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.TotalCharges.MonetaryValue");
    const wchar_t *ratedPackageWeight = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.Weight");
    const wchar_t *ratedPackageBillingWeightUnitOfMeasurementCode = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Code");
    const wchar_t *ratedPackageBillingWeightUnitOfMeasurementDescription = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Description");
    const wchar_t *ratedPackageBillingWeightWeight = json.stringOf(L"RateResponse.RatedShipment.RatedPackage.BillingWeight.Weight");

    const wchar_t *Code = 0;
    const wchar_t *Description = 0;

    i = 0;
    count_i = json.SizeOfArray(L"RateResponse.Response.Alert");
    while (i < count_i) {
        json.put_I(i);
        Code = json.stringOf(L"RateResponse.Response.Alert[i].Code");
        Description = json.stringOf(L"RateResponse.Response.Alert[i].Description");
        i = i + 1;
    }

    i = 0;
    count_i = json.SizeOfArray(L"RateResponse.RatedShipment.RatedShipmentAlert");
    while (i < count_i) {
        json.put_I(i);
        Code = json.stringOf(L"RateResponse.RatedShipment.RatedShipmentAlert[i].Code");
        Description = json.stringOf(L"RateResponse.RatedShipment.RatedShipmentAlert[i].Description");
        i = i + 1;
    }
    }

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.