0
votes

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?

2
Looks like invalid UTF-8. Try IO.inspect(body). Does that URL return a binary file?Dogbert
@Dogbert, text. now it returns <<60, 33, 100, 111, 99..... How can I decode that to a text?user7040064
<<60, 33, 100, 111, 99>> #=> "<!doc" looks like you're fetching an HTML page? That page seems to have invalid UTF-8 content somewhere. Would it be possible to share the actual URL? In any case, if you just want to write that to the terminal, you can do IO.binwrite(body).Dogbert
@Dogbert, I want to parse it. How can I decode it to a text to be able to parse, will binwrite work for that?user7040064
What do you mean by "parse" here? Are you going to use some Elixir/Erlang HTML Parser library? Something else? IO.binwrite will just let you write the body to a file or stdout and fix the original problem in the question that IO.puts doesn't print the body.Dogbert

2 Answers

3
votes

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)
1
votes

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