Skip to content

Commit

Permalink
Merge pull request #18 from bcardarella/master
Browse files Browse the repository at this point in the history
Fix for non-json encoded payloads
  • Loading branch information
andrewvy authored Oct 25, 2017
2 parents d9b55bb + 0d62e17 commit 0940655
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ defmodule ChromeRemoteInterface.HTTP do
defp handle_response({:ok, status_code, _response_headers, client_ref}) do
with true <- status_code >= 200 && status_code < 300,
{:ok, body} <- :hackney.body(client_ref),
{:ok, json} <- format_body(body) |> Poison.decode() do
{:ok, formatted_body} <- format_body(body),
{:ok, json} <- decode(formatted_body) do
{:ok, json}
else
error -> error
Expand All @@ -43,6 +44,13 @@ defmodule ChromeRemoteInterface.HTTP do
{:error, reason}
end

defp format_body(""), do: "{}"
defp format_body(body), do: body
defp format_body(""), do: format_body("{}")
defp format_body(body), do: {:ok, body}

defp decode(body) do
case Poison.decode(body) do
{:ok, json} -> {:ok, json}
{:error, _reason} -> {:ok, body}
end
end
end

0 comments on commit 0940655

Please sign in to comment.