Zig
Zig
MaxMind IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the MaxMind GeoIP2 Precision Web Service.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
http.setLogin("MAXMIND_ACCOUNT_ID");
http.setPassword("MAXMIND_LICENSE_KEY");
http.setAccept("application/json");
// Lookup an IPv4 address: 149.250.207.170 (this was a randomly chosen address)
const json_str = http.quickGetStr(alloc, "https://geoip.maxmind.com/geoip/v2.1/country/149.250.207.170") catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.setEmitCompact(false);
try json.load(json_str);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Sample output:
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "continent": {
// "code": "EU",
// "geoname_id": 6255148,
// "names": {
// "ja": "?????",
// "pt-BR": "Europa",
// "ru": "??????",
// "zh-CN": "??",
// "de": "Europa",
// "en": "Europe",
// "es": "Europa",
// "fr": "Europe"
// }
// },
// "country": {
// "is_in_european_union": true,
// "iso_code": "DE",
// "geoname_id": 2921044,
// "names": {
// "fr": "Allemagne",
// "ja": "????????",
// "pt-BR": "Alemanha",
// "ru": "????????",
// "zh-CN": "??",
// "de": "Deutschland",
// "en": "Germany",
// "es": "Alemania"
// }
// },
// "maxmind": {
// "queries_remaining": 49999
// },
// "registered_country": {
// "is_in_european_union": true,
// "iso_code": "DE",
// "geoname_id": 2921044,
// "names": {
// "es": "Alemania",
// "fr": "Allemagne",
// "ja": "????????",
// "pt-BR": "Alemanha",
// "ru": "????????",
// "zh-CN": "??",
// "de": "Deutschland",
// "en": "Germany"
// }
// },
// "traits": {
// "ip_address": "149.250.207.170"
// }
// }
//
//
_ = try json.stringOf(alloc, "continent.code");
_ = json.intOf("continent.geoname_id");
_ = try json.stringOf(alloc, "continent.names.ja");
_ = try json.stringOf(alloc, "continent.names.pt-BR");
_ = try json.stringOf(alloc, "continent.names.ru");
_ = try json.stringOf(alloc, "continent.names.zh-CN");
_ = try json.stringOf(alloc, "continent.names.de");
_ = try json.stringOf(alloc, "continent.names.en");
_ = try json.stringOf(alloc, "continent.names.es");
_ = try json.stringOf(alloc, "continent.names.fr");
_ = json.boolOf("country.is_in_european_union");
_ = try json.stringOf(alloc, "country.iso_code");
_ = json.intOf("country.geoname_id");
_ = try json.stringOf(alloc, "country.names.fr");
_ = try json.stringOf(alloc, "country.names.ja");
_ = try json.stringOf(alloc, "country.names.pt-BR");
_ = try json.stringOf(alloc, "country.names.ru");
_ = try json.stringOf(alloc, "country.names.zh-CN");
_ = try json.stringOf(alloc, "country.names.de");
_ = try json.stringOf(alloc, "country.names.en");
_ = try json.stringOf(alloc, "country.names.es");
_ = json.intOf("maxmind.queries_remaining");
_ = json.boolOf("registered_country.is_in_european_union");
_ = try json.stringOf(alloc, "registered_country.iso_code");
_ = json.intOf("registered_country.geoname_id");
_ = try json.stringOf(alloc, "registered_country.names.es");
_ = try json.stringOf(alloc, "registered_country.names.fr");
_ = try json.stringOf(alloc, "registered_country.names.ja");
_ = try json.stringOf(alloc, "registered_country.names.pt-BR");
_ = try json.stringOf(alloc, "registered_country.names.ru");
_ = try json.stringOf(alloc, "registered_country.names.zh-CN");
_ = try json.stringOf(alloc, "registered_country.names.de");
_ = try json.stringOf(alloc, "registered_country.names.en");
_ = try json.stringOf(alloc, "traits.ip_address");
}