1 min read

Resolving URLs in Elixir

When inspecting the HTML code for a website, you may have noticed that there are two types of URLs: absolute and relative. Absolute URLs contain all the information you need to get to a certain page, while relative URLs provide the navigation information relative to the current page. Relative URLs are better when it comes to reducing the size of the HTML, but they make sense only in relation to a known absolute URL.

There are certain cases when you need to resolve the absolute URLs from a relative one. For instance when building a web crawler. In Elixir this can be easily achieved with URI.merge/2.

iex> URI.merge("https://books.com", "/shelf/fiction") |> to_string
"https://books.com/shelf/fiction"

iex> URI.merge("https://books.com/shelf/fiction", "../../") |> to_string
"https://books.com/"

iex> URI.merge("https://books.com/shelf/fiction/", "./../cooking") |> to_string
"https://books.com/shelf/cooking"