问题
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