2016-10-26 79 views
0

我創建了一個項目:如何導入HTTPotion成靈藥項目

$ 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 

什麼不見​​了?我沒有靈丹妙藥的經驗。

+0

嘗試在'mix.exs'中的'applications'中添加':httpotion':'[applications:[:logger,:httpotion]]''。 – Dogbert

+0

謝謝@Dogbert我更新了這個問題,但錯誤是一樣的 – ademar111190

回答

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 

我認爲它會引發異常,如果調用編譯時間。