Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Jason.DecodeError, 400 in all requests #93

Closed
lurodrigo opened this issue Nov 9, 2021 · 3 comments
Closed

Getting Jason.DecodeError, 400 in all requests #93

lurodrigo opened this issue Nov 9, 2021 · 3 comments

Comments

@lurodrigo
Copy link

lurodrigo commented Nov 9, 2021

Hi! Thanks for the great work.

Running ethereum 0.7.1, httppoison 1.8.0

I keep getting this error

{:error,
 {:invalid_json,
  %Jason.DecodeError{
    data: "<html>\r\n<head><title>400 Bad Request</title></head>\r\n<body>\r\n<center><h1>400 Bad Request</h1></center>\r\n<hr><center>openresty</center>\r\n</body>\r\n</html>\r\n",
    position: 0,
    token: nil
  }}}

for whatever request I make, using the HTTPClient provided. I'm using a blockdaemon node.

I cloned the repo and added a IO.inspect after the httppoison request:

{:ok,
 %HTTPoison.Response{
   body: "<html>\r\n<head><title>400 Bad Request</title></head>\r\n<body>\r\n<center><h1>400 Bad Request</h1></center>\r\n<hr><center>openresty</center>\r\n</body>\r\n</html>\r\n",
   headers: [
     {"Date", "Tue, 09 Nov 2021 00:10:54 GMT"},
     {"Content-Type", "text/html"},
     {"Content-Length", "154"},
     {"Connection", "close"},
     {"Server", "nginx"}
   ],
   request: %HTTPoison.Request{
     body: "{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"eth_getBlockByNumber\",\"params\":[\"0x0\",false]}",
     headers: [{"Content-Type", "application/json"}],
     method: :post,
     options: [],
     params: %{},
     url: "https://ethshared.bdnodes.net?auth={{KEY}}"
   },
   request_url: "https://ethshared.bdnodes.net?auth={{KEY}}",
   status_code: 400
 }}

I suspect this may be a problem with HTTPoison but couldn't figure out why. I wrote a BaseClient implementation that's almost the same as HttpClient, but uses Tesla instead, and it works, so the problem is probably not my node.

defmodule MyEthereumexClient do
  @moduledoc false

  use Ethereumex.Client.BaseClient
  alias Ethereumex.Config

  @type opt :: {:url, String.t()}
  @type empty_response :: :empty_response
  @type invalid_json :: {:invalid_json, any()}
  @type http_client_error :: {:error, empty_response() | invalid_json() | any()}

  @spec post_request(binary(), [opt()]) :: {:ok, any()} | http_client_error()
  def post_request(payload, opts) do
    url = Keyword.get(opts, :url) || Config.rpc_url()
    [base_url, query] = String.split(url, "?")
    %{"auth" => auth} = URI.decode_query(query)

    client =
      Tesla.client([
        {Tesla.Middleware.BaseUrl, base_url},
        {Tesla.Middleware.Headers, [{"content-type", "application/json"}]}
      ])

    case Tesla.post(client, "", payload, query: [auth: auth]) do
      {:ok, response} ->
        %Tesla.Env{body: body, status: code} = response
        decode_body(body, code)

      error ->
        error
    end
  end

  @spec decode_body(binary(), integer()) :: {:ok, any()} | http_client_error()
  defp decode_body(body, code) do
    case Jason.decode(body) do
      {:ok, decoded_body} ->
        case {code, decoded_body} do
          {200, %{"error" => error}} -> {:error, error}
          {200, result = [%{} | _]} -> {:ok, format_batch(result)}
          {200, %{"result" => result}} -> {:ok, result}
          _ -> {:error, decoded_body}
        end

      {:error, %Jason.DecodeError{data: ""}} ->
        {:error, :empty_response}

      {:error, error} ->
        {:error, {:invalid_json, error}}
    end
  end
end

I'm quite happy with the custom impl, but perhaps other people are having the same problems as me, and we could try to solve it.

@ayrat555
Copy link
Member

@lurodrigo Is it possible for you to share access to this node? I can test locally. you can contact me in telegram. @ayrat555

@lurodrigo
Copy link
Author

Done!

@ayrat555
Copy link
Member

fixed by #94

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants