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

Excel Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
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

 

 

 

(Excel) 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.

Download Excel Class Modules

Chilkat Excel Class Modules

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

Dim http As Chilkat.Http
Set http = Chilkat.NewHttp

' This is the testing endpoint for the Rate Package JSON REST API:

url = "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
Dim json As Chilkat.JsonObject
Set json = Chilkat.NewJsonObject
success = json.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME")
success = json.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD")
success = json.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY")
success = json.UpdateString("RateRequest.Request.RequestOption","Rate")
success = json.UpdateString("RateRequest.Request.TransactionReference.CustomerContext","Your Customer Context")

json.PathPrefix = "RateRequest.Shipment.Shipper."
success = json.UpdateString("Name","Apple Store, Michigan Ave")
success = json.UpdateString("ShipperNumber","1234")

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

json.PathPrefix = ""
success = json.UpdateString("RateRequest.Shipment.ShipTo.Name","Apple HQ")

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

json.PathPrefix = ""
success = json.UpdateString("RateRequest.Shipment.ShipFrom.Name","Apple Store, Michigan Ave")

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

json.PathPrefix = ""
success = json.UpdateString("RateRequest.Shipment.Service.Code","03")
success = json.UpdateString("RateRequest.Shipment.Service.Description","Service Code Description")

json.PathPrefix = "RateRequest.Shipment.Package.PackagingType."
success = json.UpdateString("Code","02")
success = json.UpdateString("Description","Rate")

json.PathPrefix = "RateRequest.Shipment.Package.Dimensions."
success = json.UpdateString("UnitOfMeasurement.Code","IN")
success = json.UpdateString("UnitOfMeasurement.Description","inches")
success = json.UpdateString("Length","5")
success = json.UpdateString("Width","4")
success = json.UpdateString("Height","3")

json.PathPrefix = "RateRequest.Shipment.Package.PackageWeight."
success = json.UpdateString("UnitOfMeasurement.Code","Lbs")
success = json.UpdateString("UnitOfMeasurement.Description","pounds")
success = json.UpdateString("Weight","1")

json.PathPrefix = ""
success = json.UpdateString("RateRequest.Shipment.ShipmentRatingOptions.NegotiatedRatesIndicator","")

json.EmitCompact = False
Debug.Print json.Emit()

Dim sb As Chilkat.StringBuilder
Set sb = Chilkat.NewStringBuilder

Set resp = http.PostJson3(url,"application/json",json)
If (http.LastMethodSuccess <> True) Then
    Debug.Print http.LastErrorText
    Exit Sub
End If

Debug.Print "status = "; resp.StatusCode

' A 200 response status indicate success.
If (resp.StatusCode <> 200) Then
    Debug.Print resp.BodyStr
    Debug.Print "Failed."

    Exit Sub
End If

success = json.Load(resp.BodyStr)
json.EmitCompact = False
Debug.Print 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.


statusCode = json.StringOf("RateResponse.Response.ResponseStatus.Code")

statusDescription = json.StringOf("RateResponse.Response.ResponseStatus.Description")

customerContext = json.StringOf("RateResponse.Response.TransactionReference.CustomerContext")

serviceCode = json.StringOf("RateResponse.RatedShipment.Service.Code")

serviceDescription = json.StringOf("RateResponse.RatedShipment.Service.Description")

billingWeightUnitOfMeasurementCode = json.StringOf("RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Code")

billingWeightUnitOfMeasurementDescription = json.StringOf("RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Description")

billingWeightWeight = json.StringOf("RateResponse.RatedShipment.BillingWeight.Weight")

transportationChargesCurrencyCode = json.StringOf("RateResponse.RatedShipment.TransportationCharges.CurrencyCode")

transportationChargesMonetaryValue = json.StringOf("RateResponse.RatedShipment.TransportationCharges.MonetaryValue")

serviceOptionsChargesCurrencyCode = json.StringOf("RateResponse.RatedShipment.ServiceOptionsCharges.CurrencyCode")

serviceOptionsChargesMonetaryValue = json.StringOf("RateResponse.RatedShipment.ServiceOptionsCharges.MonetaryValue")

totalChargesCurrencyCode = json.StringOf("RateResponse.RatedShipment.TotalCharges.CurrencyCode")

totalChargesMonetaryValue = json.StringOf("RateResponse.RatedShipment.TotalCharges.MonetaryValue")

ratedPackageTransportationChargesCurrencyCode = json.StringOf("RateResponse.RatedShipment.RatedPackage.TransportationCharges.CurrencyCode")

ratedPackageTransportationChargesMonetaryValue = json.StringOf("RateResponse.RatedShipment.RatedPackage.TransportationCharges.MonetaryValue")

ratedPackageServiceOptionsChargesCurrencyCode = json.StringOf("RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.CurrencyCode")

ratedPackageServiceOptionsChargesMonetaryValue = json.StringOf("RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.MonetaryValue")

ratedPackageTotalChargesCurrencyCode = json.StringOf("RateResponse.RatedShipment.RatedPackage.TotalCharges.CurrencyCode")

ratedPackageTotalChargesMonetaryValue = json.StringOf("RateResponse.RatedShipment.RatedPackage.TotalCharges.MonetaryValue")

ratedPackageWeight = json.StringOf("RateResponse.RatedShipment.RatedPackage.Weight")

ratedPackageBillingWeightUnitOfMeasurementCode = json.StringOf("RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Code")

ratedPackageBillingWeightUnitOfMeasurementDescription = json.StringOf("RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Description")

ratedPackageBillingWeightWeight = json.StringOf("RateResponse.RatedShipment.RatedPackage.BillingWeight.Weight")

i = 0
count_i = json.SizeOfArray("RateResponse.Response.Alert")
Do While i < count_i
    json.I = i
    Code = json.StringOf("RateResponse.Response.Alert[i].Code")
    Description = json.StringOf("RateResponse.Response.Alert[i].Description")
    i = i + 1
Loop
i = 0
count_i = json.SizeOfArray("RateResponse.RatedShipment.RatedShipmentAlert")
Do While i < count_i
    json.I = i
    Code = json.StringOf("RateResponse.RatedShipment.RatedShipmentAlert[i].Code")
    Description = json.StringOf("RateResponse.RatedShipment.RatedShipmentAlert[i].Description")
    i = i + 1
Loop

 

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