Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir “长生不老药”;case子句错误“;而模式匹配HTTPoison响应_Elixir_Phoenix Framework_Httpoison - Fatal编程技术网

Elixir “长生不老药”;case子句错误“;而模式匹配HTTPoison响应

Elixir “长生不老药”;case子句错误“;而模式匹配HTTPoison响应,elixir,phoenix-framework,httpoison,Elixir,Phoenix Framework,Httpoison,我正在使用Httpoison执行一个get请求,并希望使用case语句对响应进行模式匹配。代码如下: def current() do case HTTPoison.get!(@api_url) do {:ok, %HTTPoison.Response{body: body, status_code: 200}} -> IO.puts body {:error, %HTTPoison.Error{reason: reason}} ->

我正在使用Httpoison执行一个get请求,并希望使用case语句对响应进行模式匹配。代码如下:

  def current() do
    case HTTPoison.get!(@api_url) do
      {:ok, %HTTPoison.Response{body: body, status_code: 200}} ->
        IO.puts body
      {:error, %HTTPoison.Error{reason: reason}} ->
        IO.inspect reason
    end
  end
当状态代码为200时,打印正文。 出现错误时,检查原因

我从服务器上得到这样的响应

%HTTPoison.Response{body: "{\"USD\":10067.08}", headers: <<removed for readability>>, status_code: 200}
%HTTPoison.Response{body:{\'USD\':10067.08},标题:,状态\代码:200}
以及,(CaseClauseError)无case子句匹配的错误:


当我收到一个正文和状态代码为200的响应时,为什么会出现“no子句”错误?

问题在于
获取之后

HTTPoison.get!(@api_url)
将返回
%HTTPoison.Response{body:body,…}
或引发异常

如果您想要
{:ok,%HTTPoison.Response{body:body,…}
,请使用
HTTPoison.get(@api\u url)
(不带

或者:

def current() do
    case HTTPoison.get(@api_url) do
      {:ok, %HTTPoison.Response{body: body, status_code: 200}} ->
        IO.puts body
      {:error, %HTTPoison.Error{reason: reason}} ->
        IO.inspect reason
    end
end

def current() do
    %HTTPoison.Response{body: body, status_code: 200}} = HTTPoison.get!(@api_url) 
    IO.puts body           
end