Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
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
Misc
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
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) 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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sUrl
    Variant vJson
    Handle hoJson
    Handle hoSb
    Variant vResp
    Handle hoResp
    String sStatusCode
    String sStatusDescription
    String sCustomerContext
    String sInquiryNumberCode
    String sInquiryNumberDescription
    String sInquiryNumberValue
    String sShipmentTypeCode
    String sShipmentTypeDescription
    String sShipperNumber
    String sServiceCode
    String sServiceDescription
    String sReferenceNumberCode
    String sReferenceNumberValue
    String sPickupDate
    String sTrackingNumber
    String sUnitOfMeasurementCode
    String sWeight
    String sDisclaimer
    Integer i
    Integer iActivityCount
    String sCity
    String sProvinceCode
    String sPostalCode
    String sCountryCode
    String sLocationCode
    String sLocationDescription
    String sLocationSignedForByName
    String sActivityStatusType
    String sActivityStatusDescription
    String sActivityStatusCode
    String sActivityDate
    String sActivityTime
    Integer iTypeCode
    String sTypeDescription
    String sDocumentContent
    String sDocumentFormatCode
    String sDocumentFormatDescription
    Handle hoSbImagePath
    Handle hoImageData
    Integer iRefnumCount
    String sRefnumCode
    String sRefnumValue
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // This is the testing endpoint for the tracking API:
    Move "https://wwwcie.ups.com/rest/Track" To sUrl

    // 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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "UPSSecurity.UsernameToken.Username" "UPS_USERNAME" To iSuccess
    Get ComUpdateString Of hoJson "UPSSecurity.UsernameToken.Password" "UPS_PASSWORD" To iSuccess
    Get ComUpdateString Of hoJson "UPSSecurity.ServiceAccessToken.AccessLicenseNumber" "UPS_ACCESS_KEY" To iSuccess
    // Request all activity...
    Get ComUpdateString Of hoJson "TrackRequest.Request.RequestOption" "activity" To iSuccess
    Get ComUpdateString Of hoJson "TrackRequest.Request.TransactionReference.CustomerContext" "Your Test Case Summary Description" To iSuccess
    Get ComUpdateString Of hoJson "TrackRequest.InquiryNumber" "1Z12345E0205271688" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoJson to vJson
    Get ComPostJson3 Of hoHttp sUrl "application/json" vJson To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln "status = " iTemp1

    // A 200 response status indicate success.
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Send Destroy of hoResp
        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJson sTemp1 To iSuccess
    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    // {
    //   "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.

    Get ComStringOf Of hoJson "TrackResponse.Response.ResponseStatus.Code" To sStatusCode
    Get ComStringOf Of hoJson "TrackResponse.Response.ResponseStatus.Description" To sStatusDescription
    Showln "statusCode: " sStatusCode
    Showln "statusDescription" sStatusDescription
    Get ComStringOf Of hoJson "TrackResponse.Response.TransactionReference.CustomerContext" To sCustomerContext
    Get ComStringOf Of hoJson "TrackResponse.Shipment.InquiryNumber.Code" To sInquiryNumberCode
    Get ComStringOf Of hoJson "TrackResponse.Shipment.InquiryNumber.Description" To sInquiryNumberDescription
    Get ComStringOf Of hoJson "TrackResponse.Shipment.InquiryNumber.Value" To sInquiryNumberValue
    Get ComStringOf Of hoJson "TrackResponse.Shipment.ShipmentType.Code" To sShipmentTypeCode
    Get ComStringOf Of hoJson "TrackResponse.Shipment.ShipmentType.Description" To sShipmentTypeDescription
    Get ComStringOf Of hoJson "TrackResponse.Shipment.ShipperNumber" To sShipperNumber
    Get ComStringOf Of hoJson "TrackResponse.Shipment.Service.Code" To sServiceCode
    Get ComStringOf Of hoJson "TrackResponse.Shipment.Service.Description" To sServiceDescription
    Get ComStringOf Of hoJson "TrackResponse.Shipment.ReferenceNumber.Code" To sReferenceNumberCode
    Get ComStringOf Of hoJson "TrackResponse.Shipment.ReferenceNumber.Value" To sReferenceNumberValue
    Get ComStringOf Of hoJson "TrackResponse.Shipment.PickupDate" To sPickupDate
    Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.TrackingNumber" To sTrackingNumber
    Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code" To sUnitOfMeasurementCode
    Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.PackageWeight.Weight" To sWeight
    Get ComStringOf Of hoJson "TrackResponse.Disclaimer" To sDisclaimer

    Move 0 To i
    Get ComSizeOfArray Of hoJson "TrackResponse.Shipment.Package.Activity" To iActivityCount
    Showln "activityCount: " iActivityCount

    While (i < iActivityCount)
        Showln "-- activity " i
        Set ComI Of hoJson To i
        Get ComHasMember Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City" To bTemp1
        If (bTemp1 = True) Begin
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City" To sCity
            Showln "city: " sCity
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode" To sProvinceCode
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode" To sPostalCode
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode" To sCountryCode
        End

        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code" To sLocationCode
        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description" To sLocationDescription
        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName" To sLocationSignedForByName

        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Status.Type" To sActivityStatusType
        Showln "activityStatusType: " sActivityStatusType
        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Status.Description" To sActivityStatusDescription
        Showln "activityStatusDescription: " sActivityStatusDescription
        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Status.Code" To sActivityStatusCode
        Showln "activityStatusCode: " sActivityStatusCode

        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Date" To sActivityDate
        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Time" To sActivityTime

        Get ComHasMember Of hoJson "TrackResponse.Shipment.Package.Activity[i].Document" To bTemp1
        If (bTemp1 = True) Begin
            Get ComIntOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Document.Type.Code" To iTypeCode
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Document.Type.Description" To sTypeDescription
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Document.Content" To sDocumentContent
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Document.Format.Code" To sDocumentFormatCode
            // Format description would be something like "GIF" for a signature image.
            Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.Activity[i].Document.Format.Description" To sDocumentFormatDescription

            // 01 - Signature Image
            // 02 - Delivery Receipt
            // 03 - Free Astray
            // 04 - POD
            If (iTypeCode = 1) Begin
                // We have a signature image.  Get the image data and save to a file.
                Get Create (RefClass(cComChilkatStringBuilder)) To hoSbImagePath
                If (Not(IsComObjectCreated(hoSbImagePath))) Begin
                    Send CreateComObject of hoSbImagePath
                End
                Get ComAppend Of hoSbImagePath "qa_output/sig_" To iSuccess
                Get ComAppend Of hoSbImagePath sTrackingNumber To iSuccess
                Get ComAppend Of hoSbImagePath "." To iSuccess
                Get ComAppend Of hoSbImagePath sDocumentFormatDescription To iSuccess
                Get Create (RefClass(cComChilkatBinData)) To hoImageData
                If (Not(IsComObjectCreated(hoImageData))) Begin
                    Send CreateComObject of hoImageData
                End
                Get ComAppendEncoded Of hoImageData sDocumentContent "base64" To iSuccess
                // Write to "qa_output/sig_1Z12345E0205271688.GIF"
                Get ComGetAsString Of hoSbImagePath To sTemp1
                Get ComWriteFile Of hoImageData sTemp1 To iSuccess
            End

        End

        Move (i + 1) To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJson "TrackResponse.Shipment.Package.ReferenceNumber" To iRefnumCount
    While (i < iRefnumCount)
        Set ComI Of hoJson To i
        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.ReferenceNumber[i].Code" To sRefnumCode
        Get ComStringOf Of hoJson "TrackResponse.Shipment.Package.ReferenceNumber[i].Value" To sRefnumValue
        Move (i + 1) To i
    Loop

    Showln "Success."

    Send Destroy of hoResp


End_Procedure

 

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