Patuda Patuda - 1 month ago 22
HTTP Question

Unable to read body returned by HTTPoison

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?

Answer

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)