bartblast

bartblast

Creator of Hologram

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} …

Most Liked

l00ker

l00ker

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

Marcus

Marcus

The function just need the hostname:

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

hubertlepicki

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}}

Last Post!

bartblast

bartblast

Creator of Hologram

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

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement