bartblast
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
Leave off the protocol (http://) and use single quotes around the domain like:
:inet.getaddr('www.google.com', :inet)
Marcus
The function just need the hostname:
iex(11)> :inet.getaddr('www.google.com', :inet)
{:ok, {142, 250, 185, 100}}
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
Awesome, it works - yeah the problem was string instead of charlist. Looks like I need to improve my Erlang knowledge… Thanks @l00ker, @Marcus, @hubertlepicki !
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









