Unable to read body returned by HTTPoison

时间:2016-10-20 13:05:05

标签: http elixir

I have this:

HTTPoison.start
case HTTPoison.get("some url") do
  {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
    IO.puts(body)

Which produces an exception:

Generated escript grex_cli with MIX_ENV=dev
** (ArgumentError) argument error
    (stdlib) :io.put_chars(#PID<0.49.0>, :unicode, [<<60, 33, 100, 111,..... 58, ...>>, 10])
    (elixir) lib/kernel/cli.ex:76: anonymous fn/3 in Kernel.CLI.exec_fun/2

How can I read the body and print it?

2 个答案:

答案 0 :(得分:3)

IO.puts因错误而失败,因为body此处不是有效的UTF-8数据。如果要将非UTF-8二进制文件写入stdout,可以使用IO.binwrite而不是IO.puts

IO.binwrite(body)

答案 1 :(得分:0)

当我获得压缩内容时出现此错误。

所以我在打印之前解压缩它:

IO.puts :zlib.gunzip(body)

您还可以在unzip之前检查身体是否被压缩:https://github.com/edgurgel/httpoison/issues/81#issuecomment-219920101