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

Tcl 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
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
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

 

 

 

(Tcl) 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 Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

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

set http [new_CkHttp]

# This is the testing endpoint for the Rate Package JSON REST API:
set 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
set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "UPSSecurity.UsernameToken.Username" "UPS_USERNAME"
CkJsonObject_UpdateString $json "UPSSecurity.UsernameToken.Password" "UPS_PASSWORD"
CkJsonObject_UpdateString $json "UPSSecurity.ServiceAccessToken.AccessLicenseNumber" "UPS_ACCESS_KEY"
CkJsonObject_UpdateString $json "RateRequest.Request.RequestOption" "Rate"
CkJsonObject_UpdateString $json "RateRequest.Request.TransactionReference.CustomerContext" "Your Customer Context"

CkJsonObject_put_PathPrefix $json "RateRequest.Shipment.Shipper."
CkJsonObject_UpdateString $json "Name" "Apple Store, Michigan Ave"
CkJsonObject_UpdateString $json "ShipperNumber" "1234"

CkJsonObject_put_PathPrefix $json "RateRequest.Shipment.Shipper.Address."
CkJsonObject_UpdateString $json "AddressLine[0]" "401 North Michigan Avenue"
CkJsonObject_UpdateString $json "City" "Chicago"
CkJsonObject_UpdateString $json "StateProvinceCode" "IL"
CkJsonObject_UpdateString $json "PostalCode" "60611"
CkJsonObject_UpdateString $json "CountryCode" "US"

CkJsonObject_put_PathPrefix $json ""
CkJsonObject_UpdateString $json "RateRequest.Shipment.ShipTo.Name" "Apple HQ"

CkJsonObject_put_PathPrefix $json "RateRequest.Shipment.ShipTo.Address."
CkJsonObject_UpdateString $json "AddressLine[0]" "One Infinite Loop"
CkJsonObject_UpdateString $json "City" "Cupertino"
CkJsonObject_UpdateString $json "StateProvinceCode" "CA"
CkJsonObject_UpdateString $json "PostalCode" "95014"
CkJsonObject_UpdateString $json "CountryCode" "US"

CkJsonObject_put_PathPrefix $json ""
CkJsonObject_UpdateString $json "RateRequest.Shipment.ShipFrom.Name" "Apple Store, Michigan Ave"

CkJsonObject_put_PathPrefix $json "RateRequest.Shipment.ShipFrom.Address."
CkJsonObject_UpdateString $json "AddressLine[0]" "401 North Michigan Avenue"
CkJsonObject_UpdateString $json "City" "Chicago"
CkJsonObject_UpdateString $json "StateProvinceCode" "IL"
CkJsonObject_UpdateString $json "PostalCode" "60611"
CkJsonObject_UpdateString $json "CountryCode" "US"

CkJsonObject_put_PathPrefix $json ""
CkJsonObject_UpdateString $json "RateRequest.Shipment.Service.Code" "03"
CkJsonObject_UpdateString $json "RateRequest.Shipment.Service.Description" "Service Code Description"

CkJsonObject_put_PathPrefix $json "RateRequest.Shipment.Package.PackagingType."
CkJsonObject_UpdateString $json "Code" "02"
CkJsonObject_UpdateString $json "Description" "Rate"

CkJsonObject_put_PathPrefix $json "RateRequest.Shipment.Package.Dimensions."
CkJsonObject_UpdateString $json "UnitOfMeasurement.Code" "IN"
CkJsonObject_UpdateString $json "UnitOfMeasurement.Description" "inches"
CkJsonObject_UpdateString $json "Length" "5"
CkJsonObject_UpdateString $json "Width" "4"
CkJsonObject_UpdateString $json "Height" "3"

CkJsonObject_put_PathPrefix $json "RateRequest.Shipment.Package.PackageWeight."
CkJsonObject_UpdateString $json "UnitOfMeasurement.Code" "Lbs"
CkJsonObject_UpdateString $json "UnitOfMeasurement.Description" "pounds"
CkJsonObject_UpdateString $json "Weight" "1"

CkJsonObject_put_PathPrefix $json ""
CkJsonObject_UpdateString $json "RateRequest.Shipment.ShipmentRatingOptions.NegotiatedRatesIndicator" ""

CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

set sb [new_CkStringBuilder]

# resp is a CkHttpResponse
set resp [CkHttp_PostJson3 $http $url "application/json" $json]
if {[expr [CkHttp_get_LastMethodSuccess $http] != 1]} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkJsonObject $json
    delete_CkStringBuilder $sb
    exit
}

puts "status = [CkHttpResponse_get_StatusCode $resp]"

# A 200 response status indicate success.
if {[expr [CkHttpResponse_get_StatusCode $resp] != 200]} then {
    puts [CkHttpResponse_bodyStr $resp]
    puts "Failed."
    delete_CkHttpResponse $resp

    delete_CkHttp $http
    delete_CkJsonObject $json
    delete_CkStringBuilder $sb
    exit
}

CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp]
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

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

set statusCode [CkJsonObject_stringOf $json "RateResponse.Response.ResponseStatus.Code"]
set statusDescription [CkJsonObject_stringOf $json "RateResponse.Response.ResponseStatus.Description"]
set customerContext [CkJsonObject_stringOf $json "RateResponse.Response.TransactionReference.CustomerContext"]
set serviceCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.Service.Code"]
set serviceDescription [CkJsonObject_stringOf $json "RateResponse.RatedShipment.Service.Description"]
set billingWeightUnitOfMeasurementCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Code"]
set billingWeightUnitOfMeasurementDescription [CkJsonObject_stringOf $json "RateResponse.RatedShipment.BillingWeight.UnitOfMeasurement.Description"]
set billingWeightWeight [CkJsonObject_stringOf $json "RateResponse.RatedShipment.BillingWeight.Weight"]
set transportationChargesCurrencyCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.TransportationCharges.CurrencyCode"]
set transportationChargesMonetaryValue [CkJsonObject_stringOf $json "RateResponse.RatedShipment.TransportationCharges.MonetaryValue"]
set serviceOptionsChargesCurrencyCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.ServiceOptionsCharges.CurrencyCode"]
set serviceOptionsChargesMonetaryValue [CkJsonObject_stringOf $json "RateResponse.RatedShipment.ServiceOptionsCharges.MonetaryValue"]
set totalChargesCurrencyCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.TotalCharges.CurrencyCode"]
set totalChargesMonetaryValue [CkJsonObject_stringOf $json "RateResponse.RatedShipment.TotalCharges.MonetaryValue"]
set ratedPackageTransportationChargesCurrencyCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.TransportationCharges.CurrencyCode"]
set ratedPackageTransportationChargesMonetaryValue [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.TransportationCharges.MonetaryValue"]
set ratedPackageServiceOptionsChargesCurrencyCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.CurrencyCode"]
set ratedPackageServiceOptionsChargesMonetaryValue [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.ServiceOptionsCharges.MonetaryValue"]
set ratedPackageTotalChargesCurrencyCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.TotalCharges.CurrencyCode"]
set ratedPackageTotalChargesMonetaryValue [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.TotalCharges.MonetaryValue"]
set ratedPackageWeight [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.Weight"]
set ratedPackageBillingWeightUnitOfMeasurementCode [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Code"]
set ratedPackageBillingWeightUnitOfMeasurementDescription [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.BillingWeight.UnitOfMeasurement.Description"]
set ratedPackageBillingWeightWeight [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedPackage.BillingWeight.Weight"]

set i 0
set count_i [CkJsonObject_SizeOfArray $json "RateResponse.Response.Alert"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i
    set Code [CkJsonObject_stringOf $json "RateResponse.Response.Alert[i].Code"]
    set Description [CkJsonObject_stringOf $json "RateResponse.Response.Alert[i].Description"]
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $json "RateResponse.RatedShipment.RatedShipmentAlert"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $json $i
    set Code [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedShipmentAlert[i].Code"]
    set Description [CkJsonObject_stringOf $json "RateResponse.RatedShipment.RatedShipmentAlert[i].Description"]
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkJsonObject $json
delete_CkStringBuilder $sb

 

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