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?
答案 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