How can I find the proxy type associated with an IP address in OCaml?

To determine the proxy type of an IP address using IP2Location.io in OCaml, you can use the IP2Location.io API to retrieve geolocation and related data. For proxy types supported, please see API documentation for more info.

  1. We'll assume that you have already installed OCaml and we won't cover that here. You will also need a paid subscription to the Security plan. Subscribe now!
  2. In the command line, run the commands below to create a new executable project called example and navigate into the folder created.
    Bash
    
    dune init proj example
    cd example
    			
  3. In the command line, run the command below to install the dependencies using opam if you don't have them installed.
    Bash
    
    opam install lwt cohttp cohttp-lwt-unix yojson
    			
  4. Edit the bin/dune file and replace the code with the below codes
    OCaml
    
    (executable
     (public_name example)
     (name main)
     (libraries example lwt cohttp cohttp-lwt-unix yojson))
    			
  5. Edit the bin/main.ml file and replace the code with the below codes
    OCaml
    
    open Printf
    open Lwt
    open Cohttp
    open Cohttp_lwt_unix
    open Yojson
    
    let () =
      let ip = "8.8.8.8" in
      let key = "YOUR_API_KEY" in
      let uri =
        Uri.of_string
          ("https://api.ip2location.io/?format=json&key=" ^ key ^ "&ip=" ^ ip)
      in
      Lwt_main.run
        ( Client.get uri >>= fun (resp, body) ->
          let code = resp |> Response.status |> Code.code_of_status in
          let json_promise = body |> Cohttp_lwt.Body.to_string in
          json_promise >|= fun json_string ->
          let json = Basic.from_string json_string in
          let open Yojson.Basic.Util in
          match code with
          | 200 when member "proxy" json <> `Null ->
              json |> member "proxy" |> member "proxy_type" |> to_string
              |> printf "The proxy type for IP %s is %s.\n" ip
          | 200 ->
              printf
                "ERROR: The proxy_type field requires a paid subscription to the \
                 Security plan.\n"
          | (400 | 401) when member "error" json <> `Null ->
              json |> member "error" |> member "error_message" |> to_string
              |> printf "ERROR: %s\n"
          | _ -> printf "HTTP Code: %d\n" code )
    			
  6. In the command line, run the below commands to compile and run the codes.
    Bash
    
    dune build
    dune exec example
    			

This script will output the proxy 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.

Other Languages

Unlock Location Insights For Free

Empower your applications with accurate IP geolocation information now.

Try It for Free