def fetch(datal) do
case HTTPoison.get!("test-api.com#{data}") do
%HTTPoison.Response{body: body} ->
{:ok, body} ->
IO.puts(body)
{:error, error} ->
IO.puts("Error #{error}")
end
end
my whole code, I would like to return the body if everything is fine and an error if something goes wrong
its coming from your response, the very first one
def fetch(data) do
case HTTPoison.get!("") do
%HTTPoison.Response{body: body} ->
{:ok, body} ->
IO.puts(body)
{:error, error} ->
IO.puts("Error #{error}")
end
end
after the -> of your case you have to return something before another matching. for example IO.puts(body) as @kokolegorille suggested, or whatever you want to be returned on this specific matching.
def fetch(data) do
case HTTPoison.get!("") do
%HTTPoison.Response{body: body, status_code: 200} ->
body
%HTTPoison.Response{status_code: status_code} when status_code > 399 ->
IO.inspect(status_code, label: "STATUS CODE")
:error
response -> # do what you want...
end
end