Sample code for 30+ languages & platforms
Rust

geo.ipify.org IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the geo.ipify.org REST API.

Chilkat Rust Downloads

Rust

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

let http = chilkat::Http::new();

// Lookup an IPv4 address: 8.8.8.8
let Ok(json_str) = http.quick_get_str("https://geo.ipify.org/api/v1?apiKey=API_KEY&ipAddress=8.8.8.8") else {
    println!("{}", http.last_error_text());
    return;
};

let json = chilkat::JsonObject::new();
json.set_emit_compact(false);
let _ = json.load(&json_str).is_ok();

println!("{}", json.emit().unwrap_or_default());

// Sample output:
// Use this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

// {
//   "ip": "8.8.8.8",
//   "location": {
//     "country": "IT",
//     "region": "Lombardy",
//     "city": "Milan",
//     "lat": 45.4707,
//     "lng": 9.1889,
//     "postalCode": "20147",
//     "timezone": "+02:00"
//   }
// }

let ip = json.string_of("ip").unwrap_or_default();
let location_country = json.string_of("location.country").unwrap_or_default();
let location_region = json.string_of("location.region").unwrap_or_default();
let location_city = json.string_of("location.city").unwrap_or_default();
let location_lat = json.string_of("location.lat").unwrap_or_default();
let location_lng = json.string_of("location.lng").unwrap_or_default();
let location_postal_code = json.string_of("location.postalCode").unwrap_or_default();
let location_timezone = json.string_of("location.timezone").unwrap_or_default();