elixir - 使用 HTTPoison 初始化模块属性

标签 elixir httpoison

我正在尝试像这样初始化模块属性

  response = HTTPoison.get! url
  {:ok, response} = Poison.decode(response.body)
  @attr response

我以前用过一个文件,如下所示:

  @external_resource file = Path.join([__DIR__, "file.txt"])
  Module.register_attribute __MODULE__, :attr, accumulate: true

  for line <- File.stream!(file, [], :line) do
    @attr line
    ...

不可能对 HTTPoison 执行相同的操作并获取 API 的响应吗? 我收到此错误:

== Compilation error in file lib/module.ex ==
** (ArgumentError) argument error
    (stdlib) :ets.lookup_element(:hackney_config, :mod_metrics, 2)
    /project/deps/hackney/src/hackney_metrics.erl:27: :hackney_metrics.get_engine/0
    /project/deps/hackney/src/hackney_connect.erl:69: :hackney_connect.create_connection/5
    /project/deps/hackney/src/hackney_connect.erl:37: :hackney_connect.connect/5
    /project/deps/hackney/src/hackney.erl:316: :hackney.request/5
    lib/httpoison/base.ex:630: HTTPoison.Base.request/9
    lib/httpoison.ex:66: HTTPoison.request!/5
    lib/module.ex:4: (module)

最佳答案

相关应用程序不会在编译时自动启动。在使用之前您需要显式启动 HTTPoison:

HTTPoison.start()
response = HTTPoison.get! url
{:ok, response} = Poison.decode(response.body)
@attr response

关于elixir - 使用 HTTPoison 初始化模块属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50931455/

相关文章:

https - 向同一主机但不同主机 ip 发送 HTTPS get 请求

elixir - 使用 HTTPoison 的 HTTP 1.0 请求

rest - 使用 Elixir 和 HTTPoison 访问 Azure 存储服务 REST API

pattern-matching - elixir 函数签名中的 "<>"运算符是什么?

testing - 用于测试比较的 Elixir 最佳数据结构

unicode - "?"运算符在 Elixir 中做什么?

elixir - 使用 UUID(字符串)时使用 has_many - belongs_to 关联

Elixir、苦艾酒 如何在与苦艾酒约会后创建用户?

elixir - 获取池数