如何将HTTPotion导入elixir项目

时间:2016-10-26 18:02:29

标签: http elixir

我创建了一个项目:

$ mix new sample

我编辑了mix.exs文件:

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

和我的samplex.ex文件:

defmodule Sample do
  IO.puts "Hello World"
end

我跑:

$ mix deps.get
$ mix

我得到了:

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

直到这里完美,但如果我将sample.ex更改为:

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

我收到以下错误:

$ 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

缺少什么?我没有长生不老药的经验。

1 个答案:

答案 0 :(得分:1)

我已用提供的步骤复制了这个问题。 如果直接在模块中调用,HTTPotion.get/2会引发异常 但如果从函数调用,则工作正常。

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

检查测试

defmodule SampleTest do
  use ExUnit.Case

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

我认为如果在编译时调用它会引发异常。