user7040064
user7040064

Reputation:

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?

Upvotes: 0

Views: 767

Answers (2)

denis.peplin
denis.peplin

Reputation: 9831

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

Upvotes: 1

Dogbert
Dogbert

Reputation: 222040

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)

Upvotes: 4

Related Questions