How to resolve IP address given a URL

Does anyone know whether it’s possible in HTTPoison or Hackney to return the resolved IP address of the given URL in the response?

If it’s not possible, how can I do this?
I tried :inet.getaddr(“https://www.google.com”, :inet) but it always returns {:error, :einval} …

Leave off the protocol (http://) and use single quotes around the domain like:
:inet.getaddr('www.google.com', :inet)

5 Likes

The function just need the hostname:

iex(11)> :inet.getaddr('www.google.com', :inet)
{:ok, {142, 250, 185, 100}}
4 Likes

It returns error because you are passing an Elixir String / Binary, while it is an Erlang function expecting argument which is Charlist.

These two types are not directly compatible and you have to convert between these two often when using Erlang libraries from Elixir. Generally charlists literals will be wrapped in single quotes ’ and binaries with double quotes ". You can convert between these with to_charlist and to_string functions that are imported by default everywhere.

So, your example will work if you use single quotes and skip https:// which is a protocol. You should write:

:inet.getaddr('google.com', :inet)
{:ok, {172, 217, 16, 14}}
4 Likes

Awesome, it works - yeah the problem was string instead of charlist. Looks like I need to improve my Erlang knowledge… Thanks @l00ker, @Marcus, @hubertlepicki !

2 Likes