Mix.install and structs

That’s a good question which I’m not sure why it doesn’t work right in the root level of the script.

But for sure it works if the struct is nested within a module. So the code below works,

Mix.install([{:httpoison, "~> 1.8"}])

url = "https://httparrot.herokuapp.com/ip"

defmodule Response do
  def successful_response(resp) do
    case resp do
    {:ok, %HTTPoison.Response{status_code: 200} = response} ->
      {:ok, response}
    other ->
      {:error, "Unexpected response #{inspect(other)}"}
    end
  end
end

url
|> HTTPoison.get()
|> Response.successful_response()
|> IO.inspect()
9 Likes