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

MFC 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

 

 

 

(MFC) UPS Tracking API

Demonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkHttp.h>
#include <CkJsonObject.h>
#include <CkStringBuilder.h>
#include <CkHttpResponse.h>
#include <CkBinData.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 tracking API:
    const char *url = "https://wwwcie.ups.com/rest/Track";

    // Send an HTTP request with the following JSON body:

    // {
    //   "UPSSecurity": {
    //     "UsernameToken": {
    //       "Username": "Your Username",
    //       "Password": "Your Password"
    //     },
    //     "ServiceAccessToken": {
    //       "AccessLicenseNumber": "Your Access License Number"
    //     }
    //   },
    //   "TrackRequest": {
    //     "Request": {
    //       "RequestOption": "1",
    //       "TransactionReference": {
    //         "CustomerContext": "Your Test Case Summary Description"
    //       }
    //     },
    //     "InquiryNumber": "YourTrackingNumber"
    //   }
    // }
    // 

    // Build the above JSON.
    CkJsonObject json;
    json.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME");
    json.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD");
    json.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY");
    // Request all activity...
    json.UpdateString("TrackRequest.Request.RequestOption","activity");
    json.UpdateString("TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description");
    json.UpdateString("TrackRequest.InquiryNumber","1Z12345E0205271688");

    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");

    // {
    //   "TrackResponse": {
    //     "Response": {
    //       "ResponseStatus": {
    //         "Code": "1",
    //         "Description": "Success"
    //       },
    //       "TransactionReference": {
    //         "CustomerContext": "Your Test Case Summary Description"
    //       }
    //     },
    //     "Shipment": {
    //       "InquiryNumber": {
    //         "Code": "01",
    //         "Description": "ShipmentIdentificationNumber",
    //         "Value": "1Z12345E0205271688"
    //       },
    //       "ShipmentType": {
    //         "Code": "01",
    //         "Description": "Small Package"
    //       },
    //       "ShipperNumber": "12345E",
    //       "Service": {
    //         "Code": "002",
    //         "Description": "2ND DAY AIR"
    //       },
    //       "ReferenceNumber": {
    //         "Code": "01",
    //         "Value": "LINE4AND115"
    //       },
    //       "PickupDate": "19990608",
    //       "Package": {
    //         "TrackingNumber": "1Z12345E0205271688",
    //         "Activity": [
    //           {
    //             "ActivityLocation": {
    //               "Address": {
    //                 "City": "ANYTOWN",
    //                 "StateProvinceCode": "GA",
    // 
    //                 "PostalCode": "30340",
    //                 "CountryCode": "US"
    //               },
    //               "Code": "ML",
    //               "Description": "BACK DOOR",
    //               "SignedForByName": "JOHN DOE"
    //             },
    //             "Status": {
    //               "Type": "D",
    //               "Description": "DELIVERED",
    //               "Code": "KM"
    //             },
    //             "Date": "19990610",
    //             "Time": "120000",
    //             "Document": {
    //               "Type": {
    //                 "Code": "01",
    //                 "Description": "Signature Image"
    //               },
    //               "Content": "R0lGODdhoA ... JU9Y8RdHsRKLMVJ4MVDMREAAADs=",
    //               "Format": {
    //                 "Code": "01",
    //                 "Description": "GIF"
    //               }
    //             }
    //           },
    //           {
    //             "Status": {
    //               "Type": "M",
    //               "Description": "BILLING INFORMATION RECEIVED. SHIPMENT DATE PENDING.",
    //               "Code": "MP"
    //             },
    //             "Date": "19990608",
    //             "Time": "120000"
    //           }
    //         ],
    //         "PackageWeight": {
    //           "UnitOfMeasurement": {
    //             "Code": "LBS"
    //           },
    //           "Weight": "5.00"
    //         },
    //         "ReferenceNumber": [
    //           {
    //             "Code": "01",
    //             "Value": "LINE4AND115"
    //           },
    //           {
    //             "Code": "08",
    //             "Value": "LJ67Y5"
    //           }
    //         ]
    //       }
    //     },
    //     "Disclaimer": "You are using UPS tracking service on customer integration test environment, please switch to UPS production environment once you finish the test. The URL is https://onlinetools.ups.com/webservices/Track"
    //   }
    // }

    // Use the online tool at Generate JSON Parsing Code
    // to generate JSON parsing code.

    const char *statusCode = json.stringOf("TrackResponse.Response.ResponseStatus.Code");
    const char *statusDescription = json.stringOf("TrackResponse.Response.ResponseStatus.Description");
    strOut.append("statusCode: ");
    strOut.append(statusCode);
    strOut.append("\r\n");
    strOut.append("statusDescription");
    strOut.append(statusDescription);
    strOut.append("\r\n");
    const char *customerContext = json.stringOf("TrackResponse.Response.TransactionReference.CustomerContext");
    const char *inquiryNumberCode = json.stringOf("TrackResponse.Shipment.InquiryNumber.Code");
    const char *inquiryNumberDescription = json.stringOf("TrackResponse.Shipment.InquiryNumber.Description");
    const char *inquiryNumberValue = json.stringOf("TrackResponse.Shipment.InquiryNumber.Value");
    const char *shipmentTypeCode = json.stringOf("TrackResponse.Shipment.ShipmentType.Code");
    const char *shipmentTypeDescription = json.stringOf("TrackResponse.Shipment.ShipmentType.Description");
    const char *shipperNumber = json.stringOf("TrackResponse.Shipment.ShipperNumber");
    const char *serviceCode = json.stringOf("TrackResponse.Shipment.Service.Code");
    const char *serviceDescription = json.stringOf("TrackResponse.Shipment.Service.Description");
    const char *referenceNumberCode = json.stringOf("TrackResponse.Shipment.ReferenceNumber.Code");
    const char *referenceNumberValue = json.stringOf("TrackResponse.Shipment.ReferenceNumber.Value");
    const char *pickupDate = json.stringOf("TrackResponse.Shipment.PickupDate");
    const char *trackingNumber = json.stringOf("TrackResponse.Shipment.Package.TrackingNumber");
    const char *unitOfMeasurementCode = json.stringOf("TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code");
    const char *weight = json.stringOf("TrackResponse.Shipment.Package.PackageWeight.Weight");
    const char *disclaimer = json.stringOf("TrackResponse.Disclaimer");

    int i = 0;
    int activityCount = json.SizeOfArray("TrackResponse.Shipment.Package.Activity");
    strOut.append("activityCount: ");
    strOut.appendInt(activityCount);
    strOut.append("\r\n");

    while (i < activityCount) {
        strOut.append("-- activity ");
        strOut.appendInt(i);
        strOut.append("\r\n");
        json.put_I(i);
        if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") == true) {
            const char *city = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City");
            strOut.append("city: ");
            strOut.append(city);
            strOut.append("\r\n");
            const char *provinceCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode");
            const char *postalCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode");
            const char *countryCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode");
        }

        const char *locationCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code");
        const char *locationDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description");
        const char *locationSignedForByName = json.stringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName");

        const char *activityStatusType = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Type");
        strOut.append("activityStatusType: ");
        strOut.append(activityStatusType);
        strOut.append("\r\n");
        const char *activityStatusDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Description");
        strOut.append("activityStatusDescription: ");
        strOut.append(activityStatusDescription);
        strOut.append("\r\n");
        const char *activityStatusCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Status.Code");
        strOut.append("activityStatusCode: ");
        strOut.append(activityStatusCode);
        strOut.append("\r\n");

        const char *activityDate = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Date");
        const char *activityTime = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Time");

        if (json.HasMember("TrackResponse.Shipment.Package.Activity[i].Document") == true) {
            int typeCode = json.IntOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code");
            const char *typeDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Description");
            const char *documentContent = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Content");
            const char *documentFormatCode = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Code");
            // Format description would be something like "GIF" for a signature image.
            const char *documentFormatDescription = json.stringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Description");

            // 01 - Signature Image
            // 02 - Delivery Receipt
            // 03 - Free Astray
            // 04 - POD
            if (typeCode == 1) {
                // We have a signature image.  Get the image data and save to a file.
                CkStringBuilder sbImagePath;
                sbImagePath.Append("qa_output/sig_");
                sbImagePath.Append(trackingNumber);
                sbImagePath.Append(".");
                sbImagePath.Append(documentFormatDescription);
                CkBinData imageData;
                success = imageData.AppendEncoded(documentContent,"base64");
                // Write to "qa_output/sig_1Z12345E0205271688.GIF"
                success = imageData.WriteFile(sbImagePath.getAsString());
            }

        }

        i = i + 1;
    }

    i = 0;
    int refnumCount = json.SizeOfArray("TrackResponse.Shipment.Package.ReferenceNumber");
    while (i < refnumCount) {
        json.put_I(i);
        const char *refnumCode = json.stringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Code");
        const char *refnumValue = json.stringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Value");
        i = i + 1;
    }

    strOut.append("Success.");
    strOut.append("\r\n");

    delete resp;


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

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