I'm trying to simply display some data in a template. But apparently I'm missing a step.
request with HTTPoison
use HTTPoison.Base
@expected_fields ~w(result)
def process_url(url) do
"url.json"
end
def process_response_body(body) do
body
|> Poison.decode!
|> Map.take(@expected_fields)
|> Enum.map(fn({k ,v}) -> {String.to_atom(k), v} end)
end
controller
def index(conn, _params) do
response = ApiTest.get!("").body[:result]
render conn, "index.html", response: response
end
template
<%= @response %>
the error is: lists in Phoenix.HTML and templates may only contain integers representing bytes, binaries or other lists, got invalid entry
So where do I transform the data to a format that can be displayed in the template?
From the error message, it looks like the result
key in the response JSON is either a list of contains a list. If you want output for debugging, two common ways are to use Kernel.inspect
to get a representation like the one in iex
or Poison.encode!
to get a JSON representation:
<%= inspect(@response) %>
or
<%= Poison.encode!(@response) %>
You might want to wrap the whole thing in <pre>
tags to get nicely wrapped output in inspect
:
<pre><%= inspect(@response) %></pre>
or do that + add pretty: true
for Poison.encode!
:
<pre><%= Poison.encode!(@response, pretty: true) %></pre>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments