HTTPoison is an HTTP client for Elixir. We have already used it in the Managing dependencies recipe of Chapter 1, Command Line.
In this recipe, we will create a simple application that will take a URL and fetch the corresponding page, returning either the body or the headers of that request.
We will be using the get_pages
application. You will find it in the source code of this book. The steps are as follows:
Enter the application directory:
> cd get_pages
Fetch the dependencies and compile them:
> mix deps.get && mix deps.compile
Start the application:
> iex –S mix
To get an HTTP response and perform pattern matching on it, we will follow these steps:
Issue a request to fetch the
elixir-lang
main page and take the headers from the response:iex(1)> GetPages.get(:headers, "http://elixir-lang.com")
The result is shown in the following screenshot:
Now, we will request the main Google page and take the body from...