Sample code for 30+ languages & platforms
Zig

api.db-ip.com IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the api.db-ip.com REST API.

Chilkat Zig Downloads

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

    // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
    const json_str = http.quickGetStr(alloc, "http://api.db-ip.com/v2/free/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

    // {
    //   "ipAddress": "149.250.207.170",
    //   "continentCode": "EU",
    //   "continentName": "Europe",
    //   "countryCode": "DE",
    //   "countryName": "Germany",
    //   "stateProv": "Baden-W\u00fcrttemberg",
    //   "city": "B\u00f6blingen"
    // }

    _ = try json.stringOf(alloc, "ipAddress");
    _ = try json.stringOf(alloc, "continentCode");
    _ = try json.stringOf(alloc, "continentName");
    _ = try json.stringOf(alloc, "countryCode");
    _ = try json.stringOf(alloc, "countryName");
    _ = try json.stringOf(alloc, "stateProv");
    _ = try json.stringOf(alloc, "city");
}