To determine the usage type of an IP address using IP2Location.io in Erlang, you can use the IP2Location.io API to retrieve geolocation and related data. The usage type field specifies how the IP is being used. For usage types supported, please see API documentation for more info.
sudo apt install erlang-jiffy
-module(test).
-export([runtest/0]).
runtest() ->
Key = "YOUR_API_KEY",
IP = "8.8.8.8",
ssl:start(),
inets:start(),
MyParams = uri_string:compose_query([{"format", "json"}, {"key", Key}, {"ip", IP}]),
case httpc:request(get, {"https://api.ip2location.io/?" ++ MyParams, []}, [{ssl, [{versions, ['tlsv1.2']}]}, {autoredirect, false}], []) of
{ok, {{_, 200, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"usage_type">>, Result) of
true ->
UsageType = maps:get(<<"usage_type">>, Result),
io:format("The usage type for IP ~s is ~s.~n", [IP, UsageType]);
_ ->
io:format("ERROR: The usage_type field requires a paid subscription to the Starter plan or higher.~n", [])
end;
{ok, {{_, 400, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"error">>, Result) of
true ->
Error = maps:get(<<"error">>, Result),
io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
_ ->
io:format("ERROR: ~p~n", [Result])
end;
{ok, {{_, 401, _}, _, Body}} ->
Result = jiffy:decode(unicode:characters_to_binary(Body,unicode,utf8),[return_maps]),
case maps:is_key(<<"error">>, Result) of
true ->
Error = maps:get(<<"error">>, Result),
io:format("ERROR: ~s~n", [maps:get(<<"error_message">>, Error)]);
_ ->
io:format("ERROR: ~p~n", [Result])
end;
{error, Reason} ->
io:format("ERROR ~p~n", [Reason])
end.
erl
c(test).
test:runtest().
This script will output the usage type of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free