Skip to content

Commit 0940655

Browse files
authored
Merge pull request #18 from bcardarella/master
Fix for non-json encoded payloads
2 parents d9b55bb + 0d62e17 commit 0940655

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/http.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ defmodule ChromeRemoteInterface.HTTP do
2828
defp handle_response({:ok, status_code, _response_headers, client_ref}) do
2929
with true <- status_code >= 200 && status_code < 300,
3030
{:ok, body} <- :hackney.body(client_ref),
31-
{:ok, json} <- format_body(body) |> Poison.decode() do
31+
{:ok, formatted_body} <- format_body(body),
32+
{:ok, json} <- decode(formatted_body) do
3233
{:ok, json}
3334
else
3435
error -> error
@@ -43,6 +44,13 @@ defmodule ChromeRemoteInterface.HTTP do
4344
{:error, reason}
4445
end
4546

46-
defp format_body(""), do: "{}"
47-
defp format_body(body), do: body
47+
defp format_body(""), do: format_body("{}")
48+
defp format_body(body), do: {:ok, body}
49+
50+
defp decode(body) do
51+
case Poison.decode(body) do
52+
{:ok, json} -> {:ok, json}
53+
{:error, _reason} -> {:ok, body}
54+
end
55+
end
4856
end

0 commit comments

Comments
 (0)