ademar111190 ademar111190 - 1 month ago 12
HTTP Question

How to import a HTTPotion into an elixir project

I created an project:

$ mix new sample


I edited the
mix.exs
file:

defmodule Sample.Mixfile do
use Mix.Project

def project do
[app: :sample,
version: "0.1.0",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
end

def application do
[applications: [:logger, :httpotion]]
end

defp deps do
[{:httpotion, "~> 3.0.2"}]
end
end


and my
samplex.ex
file:

defmodule Sample do
IO.puts "Hello World"
end


I run:

$ mix deps.get
$ mix


and I get:

Compiling 1 file (.ex)
Hello World
Generated sample app


until here all perfect, but if I change the sample.ex to:

defmodule Sample do
HTTPotion.get "httpbin.org/get"
end


I get the following error:

$ mix deps.get
Running dependency resolution
All dependencies up to date
$ mix
Compiling 1 file (.ex)
warning: variable response is unused
lib/sample.ex:2


== Compilation error on file lib/sample.ex ==
** (ArgumentError) argument error
(stdlib) :ets.lookup(:ibrowse_lb, {'httpbin.org', 80})
/Users/xxx/sample/deps/ibrowse/src/ibrowse.erl:328: :ibrowse.send_req/6
lib/httpotion.ex:355: HTTPotion.request/3
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6


what is missing? I have no experience with elixir.

Answer

I've reproduced the issue with provided steps. HTTPotion.get/2 raises exception if called directly in the module but works fine if called from the function.

defmodule Sample do
  def request do
    HTTPotion.get "httpbin.org/get"
  end
end

Checked with test

defmodule SampleTest do
  use ExUnit.Case

  test "response from HTTPotion" do
    assert Sample.request
  end
end

I think it raises exception if called in compile time.