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

Tcl 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
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

 

 

 

(Tcl) Google Maps Geolocation Request

Demonstrates how make a Google Maps Geolocation REST API request.

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

# This example duplicates the following CURL request:
# curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY"

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

set rest [new_CkRest]

# Connect to the Google API REST server.
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "www.googleapis.com" $port $bTls $bAutoReconnect]

# Add the Content-Type request header.
CkRest_AddHeader $rest "Content-Type" "application/json"

# Add your API key as a query parameter
CkRest_AddQueryParam $rest "key" "YOUR_API_KEY"

# The JSON query is contained in the body of the HTTP POST.
# This is a sample query (which we'll dynamically build in this example)
# { 
#  "homeMobileCountryCode": 310,
#  "homeMobileNetworkCode": 260,
#  "radioType": "gsm",
#  "carrier": "T-Mobile",
#  "cellTowers": [
#   {
#    "cellId": 39627456,
#    "locationAreaCode": 40495,
#    "mobileCountryCode": 310,
#    "mobileNetworkCode": 260,
#    "age": 0,
#    "signalStrength": -95
#   }
#  ],
#  "wifiAccessPoints": [
#   { 
#    "macAddress": "01:23:45:67:89:AB",
#    "signalStrength": 8,
#    "age": 0,
#    "signalToNoiseRatio": -65,
#    "channel": 8
#   },
#   { 
#    "macAddress": "01:23:45:67:89:AC",
#    "signalStrength": 4,
#    "age": 0
#   }
#  ]
# }

set json [new_CkJsonObject]

CkJsonObject_AppendInt $json "homeMobileCountryCode" 310
CkJsonObject_AppendInt $json "homeMobileNetworkCode" 260
CkJsonObject_AppendString $json "radioType" "gsm"
CkJsonObject_AppendString $json "carrier" "T-Mobile"
# aCellTowers is a CkJsonArray
set aCellTowers [CkJsonObject_AppendArray $json "cellTowers"]
CkJsonArray_AddObjectAt $aCellTowers 0
# oCellTower is a CkJsonObject
set oCellTower [CkJsonArray_ObjectAt $aCellTowers 0]
CkJsonObject_AppendInt $oCellTower "cellId" 39627456
CkJsonObject_AppendInt $oCellTower "locationAreaCode" 40495
CkJsonObject_AppendInt $oCellTower "mobileCountryCode" 310
CkJsonObject_AppendInt $oCellTower "mobileNetworkCode" 260
CkJsonObject_AppendInt $oCellTower "age" 0
CkJsonObject_AppendInt $oCellTower "signalStrength" -95
delete_CkJsonObject $oCellTower

delete_CkJsonArray $aCellTowers

# aWifi is a CkJsonArray
set aWifi [CkJsonObject_AppendArray $json "wifiAccessPoints"]
CkJsonArray_AddObjectAt $aWifi 0
# oPoint is a CkJsonObject
set oPoint [CkJsonArray_ObjectAt $aWifi 0]
CkJsonObject_AppendString $oPoint "macAddress" "01:23:45:67:89:AB"
CkJsonObject_AppendInt $oPoint "signalStrength" 8
CkJsonObject_AppendInt $oPoint "age" 0
CkJsonObject_AppendInt $oPoint "signalToNoiseRatio" -65
CkJsonObject_AppendInt $oPoint "channel" 8
delete_CkJsonObject $oPoint

CkJsonArray_AddObjectAt $aWifi 1
set oPoint [CkJsonArray_ObjectAt $aWifi 1]
CkJsonObject_AppendString $oPoint "macAddress" "01:23:45:67:89:AC"
CkJsonObject_AppendInt $oPoint "signalStrength" 4
CkJsonObject_AppendInt $oPoint "age" 0
delete_CkJsonObject $oPoint

delete_CkJsonArray $aWifi

set responseJson [CkRest_fullRequestString $rest "POST" "/geolocation/v1/geolocate" [CkJsonObject_emit $json]]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkJsonObject $json
    exit
}

# When successful, the response code is 200.
if {[CkRest_get_ResponseStatusCode $rest] != 200} then {
    # Examine the request/response to see what happened.
    puts "response status code = [CkRest_get_ResponseStatusCode $rest]"
    puts "response status text = [CkRest_responseStatusText $rest]"
    puts "response header: [CkRest_responseHeader $rest]"
    puts "response JSON: $responseJson"
    puts "---"
    puts "LastRequestStartLine: [CkRest_lastRequestStartLine $rest]"
    puts "LastRequestHeader: [CkRest_lastRequestHeader $rest]"
    delete_CkRest $rest
    delete_CkJsonObject $json
    exit
}

CkJsonObject_put_EmitCompact $json 0
puts "JSON request body: [CkJsonObject_emit $json]"

# The JSON response should look like this:
# { 
#  "location": {
#   "lat": 37.4248297,
#   "lng": -122.07346549999998
#  },
#  "accuracy": 1145.0
# }

puts "JSON response: $responseJson"

set jsonResp [new_CkJsonObject]

CkJsonObject_Load $jsonResp $responseJson

# jsonLoc is a CkJsonObject
set jsonLoc [CkJsonObject_ObjectOf $jsonResp "location"]
# Any JSON value can be obtained as a string..
set latitude [CkJsonObject_stringOf $jsonLoc "lat"]
puts "latitude = $latitude"
set longitude [CkJsonObject_stringOf $jsonLoc "lng"]
puts "longitude = $longitude"
delete_CkJsonObject $jsonLoc

set accuracy [CkJsonObject_stringOf $jsonResp "accuracy"]
puts "accuracy = $accuracy"

delete_CkRest $rest
delete_CkJsonObject $json
delete_CkJsonObject $jsonResp

 

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