Unable to read body returned by HTTPoison

荒凉一梦 提交于 2019-12-12 02:16:29

问题


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?


回答1:


IO.puts fails with that error because body here is not valid UTF-8 data. If you want to write non UTF-8 binary to stdout, you can use IO.binwrite instead of IO.puts:

IO.binwrite(body)



回答2:


I got this error when I got compressed content.

So I unzipped it before printing:

IO.puts :zlib.gunzip(body)

You also can check if body is compressed before unzip: https://github.com/edgurgel/httpoison/issues/81#issuecomment-219920101



来源:https://stackoverflow.com/questions/40155304/unable-to-read-body-returned-by-httpoison

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!