Sample code for 30+ languages & platforms
Rust

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 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();

http.set_login("ACCESS_TOKEN");
http.set_password("");
http.set_accept("application/json");

// Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
let Ok(json_str) = http.quick_get_str("https://ipinfo.io/149.250.207.170") 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

// 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"
//   }
// }
// 

let ip = json.string_of("ip").unwrap_or_default();
let hostname = json.string_of("hostname").unwrap_or_default();
let city = json.string_of("city").unwrap_or_default();
let region = json.string_of("region").unwrap_or_default();
let country = json.string_of("country").unwrap_or_default();
let loc = json.string_of("loc").unwrap_or_default();
let postal = json.string_of("postal").unwrap_or_default();
let asn_asn = json.string_of("asn.asn").unwrap_or_default();
let asn_name = json.string_of("asn.name").unwrap_or_default();
let asn_domain = json.string_of("asn.domain").unwrap_or_default();
let asn_route = json.string_of("asn.route").unwrap_or_default();
let asn_type = json.string_of("asn.type").unwrap_or_default();
let company_name = json.string_of("company.name").unwrap_or_default();
let company_domain = json.string_of("company.domain").unwrap_or_default();
let company_type = json.string_of("company.type").unwrap_or_default();
let carrier_name = json.string_of("carrier.name").unwrap_or_default();
let carrier_mcc = json.string_of("carrier.mcc").unwrap_or_default();
let carrier_mnc = json.string_of("carrier.mnc").unwrap_or_default();