Go Requires Chilkat v11.0.0+
Go
Look up an IP address with the ip-api free endpoint
See more Geolocation Examples
This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.
Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat Go Downloads
success := false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
ipAddress := "8.8.8.8"
http := chilkat.NewHttp()
http.SetRequireSslCertVerify(true)
http.SetConnectTimeout(10)
http.SetReadTimeout(30)
http.SetAccept("application/json")
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
url := chilkat.NewStringBuilder()
url.Append("http://ip-api.com/json/")
url.Append(*http.UrlEncode(ipAddress))
parameters := chilkat.NewJsonObject()
parameters.UpdateString("fields","status,message,query,country,countryCode,regionName,city,lat,lon,timezone")
response := chilkat.NewHttpResponse()
success = http.HttpParams("GET",*url.GetAsString(),parameters,response)
if response.StatusCode() == 0 {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
fmt.Println("No HTTP response. Check connectivity, DNS, and TLS configuration.")
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
return
}
// Save these values if the application will make further requests.
remaining := response.GetHeaderField("X-Rl")
if *remaining == "0" {
fmt.Println("Rate limit reached. Wait ", *response.GetHeaderField("X-Ttl"), " seconds before the next request.")
}
json := chilkat.NewJsonObject()
parsed := json.Load(response.BodyStr())
if (success != true) || (response.StatusCode() < 200) || (response.StatusCode() >= 300) {
fmt.Println("Lookup failed. HTTP ", response.StatusCode())
if parsed == true {
if json.JsonTypeOf("message") == 1 {
fmt.Println(*json.StringOf("message"))
}
}
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if parsed != true {
fmt.Println("Expected a JSON object in the lookup response.")
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if json.StringOfEquals("status","success",true) != true {
fmt.Println("The service could not complete the lookup.")
if json.JsonTypeOf("message") == 1 {
fmt.Println(*json.StringOf("message"))
}
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
if (json.JsonTypeOf("query") != 1) || (json.StringOfEquals("query","",true) == true) {
fmt.Println("The response did not contain the expected IP address.")
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()
return
}
fmt.Println("IP address: ", *json.StringOf("query"))
if (json.JsonTypeOf("country") == 1) && (json.StringOfEquals("country","",true) != true) {
fmt.Println("Country: ", *json.StringOf("country"))
} else {
fmt.Println("Country: unavailable")
}
if (json.JsonTypeOf("countryCode") == 1) && (json.StringOfEquals("countryCode","",true) != true) {
fmt.Println("Country code: ", *json.StringOf("countryCode"))
} else {
fmt.Println("Country code: unavailable")
}
if (json.JsonTypeOf("regionName") == 1) && (json.StringOfEquals("regionName","",true) != true) {
fmt.Println("Region: ", *json.StringOf("regionName"))
} else {
fmt.Println("Region: unavailable")
}
if (json.JsonTypeOf("city") == 1) && (json.StringOfEquals("city","",true) != true) {
fmt.Println("City: ", *json.StringOf("city"))
} else {
fmt.Println("City: unavailable")
}
if (json.JsonTypeOf("timezone") == 1) && (json.StringOfEquals("timezone","",true) != true) {
fmt.Println("Time zone: ", *json.StringOf("timezone"))
} else {
fmt.Println("Time zone: unavailable")
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (json.JsonTypeOf("lat") == 2) && (json.JsonTypeOf("lon") == 2) {
fmt.Println("Coordinates: ", *json.StringOf("lat"), ", ", *json.StringOf("lon"))
} else {
fmt.Println("Coordinates: unavailable")
}
http.DisposeHttp()
url.DisposeStringBuilder()
parameters.DisposeJsonObject()
response.DisposeHttpResponse()
json.DisposeJsonObject()