Lua
Lua
ipinfo.io IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the ipinfo.io API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
http.login = "ACCESS_TOKEN"
http.password = ""
http.accept = "application/json"
-- Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
local jsonStr = http:quick_get_str("https://ipinfo.io/149.250.207.170")
if not http.last_method_success then
print(http.last_error_text)
return
end
local json = chilkat.JsonObject.new()
json.emit_compact = false
local success = json:load(jsonStr)
print(json:emit())
-- Sample output:
-- Use this online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
-- Note: This is the output for the free trial account.
--
-- {
-- "ip": "149.250.207.170",
-- "city": "",
-- "region": "",
-- "country": "DE",
-- "loc": "51.2993,9.4910",
-- "org": "AS15854 EntServ Deutschland GmbH"
-- }
-- A full response would look like this:
-- {
-- "ip": "66.87.125.72",
-- "hostname": "66-87-125-72.pools.spcsdns.net",
-- "city": "Southbridge",
-- "region": "Massachusetts",
-- "country": "US",
-- "loc": "42.0707,-72.0440",
-- "postal": "01550",
-- "asn": {
-- "asn": "AS10507",
-- "name": "Sprint Personal Communications Systems",
-- "domain": "spcsdns.net",
-- "route": "66.87.125.0/24",
-- "type": "isp"
-- },
-- "company": {
-- "name": "Sprint Springfield POP",
-- "domain": "sprint.com",
-- "type": "isp"
-- },
-- "carrier": {
-- "name": "Sprint",
-- "mcc": "310",
-- "mnc": "120"
-- }
-- }
--
local ip = json:string_of("ip")
local hostname = json:string_of("hostname")
local city = json:string_of("city")
local region = json:string_of("region")
local country = json:string_of("country")
local loc = json:string_of("loc")
local postal = json:string_of("postal")
local asnAsn = json:string_of("asn.asn")
local asnName = json:string_of("asn.name")
local asnDomain = json:string_of("asn.domain")
local asnRoute = json:string_of("asn.route")
local asnType = json:string_of("asn.type")
local companyName = json:string_of("company.name")
local companyDomain = json:string_of("company.domain")
local companyType = json:string_of("company.type")
local carrierName = json:string_of("carrier.name")
local carrierMcc = json:string_of("carrier.mcc")
local carrierMnc = json:string_of("carrier.mnc")