Chilkat Examples

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

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

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

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sUrl
    Variant vJson
    Handle hoJson
    Handle hoSb
    Variant vResp
    Handle hoResp
    Integer i
    Integer iCount_i
    String sStatusCode
    String sStatusDescription
    String sCustomerContext
    String sServiceCode
    String sServiceDescription
    String sBillingWeightUnitOfMeasurementCode
    String sBillingWeightUnitOfMeasurementDescription
    String sBillingWeightWeight
    String sTransportationChargesCurrencyCode
    String sTransportationChargesMonetaryValue
    String sServiceOptionsChargesCurrencyCode
    String sServiceOptionsChargesMonetaryValue
    String sTotalChargesCurrencyCode
    String sTotalChargesMonetaryValue
    String sRatedPackageTransportationChargesCurrencyCode
    String sRatedPackageTransportationChargesMonetaryValue
    String sRatedPackageServiceOptionsChargesCurrencyCode
    String sRatedPackageServiceOptionsChargesMonetaryValue
    String sRatedPackageTotalChargesCurrencyCode
    String sRatedPackageTotalChargesMonetaryValue
    String sRatedPackageWeight
    String sRatedPackageBillingWeightUnitOfMeasurementCode
    String sRatedPackageBillingWeightUnitOfMeasurementDescription
    String sRatedPackageBillingWeightWeight
    String sCode
    String sDescription
    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 Rate Package JSON REST API:
    Move "https://wwwcie.ups.com/rest/Rate" To sUrl

    // 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
    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
    Get ComUpdateString Of hoJson "RateRequest.Request.RequestOption" "Rate" To iSuccess
    Get ComUpdateString Of hoJson "RateRequest.Request.TransactionReference.CustomerContext" "Your Customer Context" To iSuccess

    Set ComPathPrefix Of hoJson To "RateRequest.Shipment.Shipper."
    Get ComUpdateString Of hoJson "Name" "Apple Store, Michigan Ave" To iSuccess
    Get ComUpdateString Of hoJson "ShipperNumber" "1234" To iSuccess

    Set ComPathPrefix Of hoJson To "RateRequest.Shipment.Shipper.Address."
    Get ComUpdateString Of hoJson "AddressLine[0]" "401 North Michigan Avenue" To iSuccess
    Get ComUpdateString Of hoJson "City" "Chicago" To iSuccess
    Get ComUpdateString Of hoJson "StateProvinceCode" "IL" To iSuccess
    Get ComUpdateString Of hoJson "PostalCode" "60611" To iSuccess
    Get ComUpdateString Of hoJson "CountryCode" "US" To iSuccess

    Set ComPathPrefix Of hoJson To ""
    Get ComUpdateString Of hoJson "RateRequest.Shipment.ShipTo.Name" "Apple HQ" To iSuccess

    Set ComPathPrefix Of hoJson To "RateRequest.Shipment.ShipTo.Address."
    Get ComUpdateString Of hoJson "AddressLine[0]" "One Infinite Loop" To iSuccess
    Get ComUpdateString Of hoJson "City" "Cupertino" To iSuccess
    Get ComUpdateString Of hoJson "StateProvinceCode" "CA" To iSuccess
    Get ComUpdateString Of hoJson "PostalCode" "95014" To iSuccess
    Get ComUpdateString Of hoJson "CountryCode" "US" To iSuccess

    Set ComPathPrefix Of hoJson To ""
    Get ComUpdateString Of hoJson "RateRequest.Shipment.ShipFrom.Name" "Apple Store, Michigan Ave" To iSuccess

    Set ComPathPrefix Of hoJson To "RateRequest.Shipment.ShipFrom.Address."
    Get ComUpdateString Of hoJson "AddressLine[0]" "401 North Michigan Avenue" To iSuccess
    Get ComUpdateString Of hoJson "City" "Chicago" To iSuccess
    Get ComUpdateString Of hoJson "StateProvinceCode" "IL" To iSuccess
    Get ComUpdateString Of hoJson "PostalCode" "60611" To iSuccess
    Get ComUpdateString Of hoJson "CountryCode" "US" To iSuccess

    Set ComPathPrefix Of hoJson To ""
    Get ComUpdateString Of hoJson "RateRequest.Shipment.Service.Code" "03" To iSuccess
    Get ComUpdateString Of hoJson "RateRequest.Shipment.Service.Description" "Service Code Description" To iSuccess

    Set ComPathPrefix Of hoJson To "RateRequest.Shipment.Package.PackagingType."
    Get ComUpdateString Of hoJson "Code" "02" To iSuccess
    Get ComUpdateString Of hoJson "Description" "Rate" To iSuccess

    Set ComPathPrefix Of hoJson To "RateRequest.Shipment.Package.Dimensions."
    Get ComUpdateString Of hoJson "UnitOfMeasurement.Code" "IN" To iSuccess
    Get ComUpdateString Of hoJson "UnitOfMeasurement.Description" "inches" To iSuccess
    Get ComUpdateString Of hoJson "Length" "5" To iSuccess
    Get ComUpdateString Of hoJson "Width" "4" To iSuccess
    Get ComUpdateString Of hoJson "Height" "3" To iSuccess

    Set ComPathPrefix Of hoJson To "RateRequest.Shipment.Package.PackageWeight."
    Get ComUpdateString Of hoJson "UnitOfMeasurement.Code" "Lbs" To iSuccess
    Get ComUpdateString Of hoJson "UnitOfMeasurement.Description" "pounds" To iSuccess
    Get ComUpdateString Of hoJson "Weight" "1" To iSuccess

    Set ComPathPrefix Of hoJson To ""
    Get ComUpdateString Of hoJson "RateRequest.Shipment.ShipmentRatingOptions.NegotiatedRatesIndicator" "" To iSuccess

    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

    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

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

    Get ComStringOf Of hoJson "RateResponse.Response.ResponseStatus.Code" To sStatusCode
    Get ComStringOf Of hoJson "RateResponse.Response.ResponseStatus.Description" To sStatusDescription
    Get ComStringOf Of hoJson "RateResponse.Response.TransactionReference.CustomerContext" To sCustomerContext
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.Service.Code" To sServiceCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.Service.Description" To sServiceDescription
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Code" To sBillingWeightUnitOfMeasurementCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Description" To sBillingWeightUnitOfMeasurementDescription
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.BillingWeight.Weight" To sBillingWeightWeight
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.TransportationCharges.CurrencyCode" To sTransportationChargesCurrencyCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.TransportationCharges.MonetaryValue" To sTransportationChargesMonetaryValue
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.ServiceOptionsCharges.CurrencyCode" To sServiceOptionsChargesCurrencyCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.ServiceOptionsCharges.MonetaryValue" To sServiceOptionsChargesMonetaryValue
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.TotalCharges.CurrencyCode" To sTotalChargesCurrencyCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.TotalCharges.MonetaryValue" To sTotalChargesMonetaryValue
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.TransportationCharges.CurrencyCode" To sRatedPackageTransportationChargesCurrencyCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.TransportationCharges.MonetaryValue" To sRatedPackageTransportationChargesMonetaryValue
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.CurrencyCode" To sRatedPackageServiceOptionsChargesCurrencyCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.MonetaryValue" To sRatedPackageServiceOptionsChargesMonetaryValue
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.TotalCharges.CurrencyCode" To sRatedPackageTotalChargesCurrencyCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.TotalCharges.MonetaryValue" To sRatedPackageTotalChargesMonetaryValue
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.Weight" To sRatedPackageWeight
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Code" To sRatedPackageBillingWeightUnitOfMeasurementCode
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Description" To sRatedPackageBillingWeightUnitOfMeasurementDescription
    Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedPackage.BillingWeight.Weight" To sRatedPackageBillingWeightWeight

    Move 0 To i
    Get ComSizeOfArray Of hoJson "RateResponse.Response.Alert" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJson To i
        Get ComStringOf Of hoJson "RateResponse.Response.Alert[i].Code" To sCode
        Get ComStringOf Of hoJson "RateResponse.Response.Alert[i].Description" To sDescription
        Move i + 1 To i
    Loop

    Move 0 To i
    Get ComSizeOfArray Of hoJson "RateResponse.RatedShipment.RatedShipmentAlert" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJson To i
        Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedShipmentAlert[i].Code" To sCode
        Get ComStringOf Of hoJson "RateResponse.RatedShipment.RatedShipmentAlert[i].Description" To sDescription
        Move i + 1 To i
    Loop



End_Procedure

 

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