<p>The code is free of syntax errors, but it looks like you need to replace the <code>rpcuser</code> and <code>rpcpassword</code> in this line:</p>

<p><code>h = BitcoinRPC.new('http://rpcuser:rpcpassword@127.0.0.1:8332')</code></p>

<p>To the actual rpcuser and rpcpassword values that are set in your <code>bitcoin.conf</code> file. </p>

<p>If that doesn't work, I'd suggest doing some debugging by opening an <code>irb</code> session and running this code to see the result of the HTTP request to the RPC service:</p>

<pre><code>require 'net/http'
require 'json'

post_body = { method: 'getblockchaininfo', params: [], id: 'jsonrpc' }.to_json

uri = URI.parse "http://rpcuser:rpcpassword@127.0.0.1:8332"

def http_post_request(uri, post_body)
  http    = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth uri.user, uri.password
  request.content_type = 'application/json'
  request.body = post_body
  http.request(request).body
end

http_post_request(uri, post_body)
</code></pre>

CC BY-SA 3.0
2017-11-02T11:29:49.720Z
2017-11-02T11:29:49.720Z
Eric Allam
<p>I don't really know Ruby but these two lines looks off to me. </p>

<pre><code>  class JSONRPCError &lt; RuntimeError; end
end
</code></pre>

<p>Two <code>end</code>s?</p>

CC BY-SA 3.0
2017-06-03T04:32:26.313Z
2017-06-03T04:32:26.313Z
m1xolyd1an
<p>I am trying the JSON-RPC call from <a href="https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#Ruby" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#Ruby</a></p>

<pre><code>require 'net/http'
require 'uri'
require 'json'

class BitcoinRPC
  def initialize(service_url)
    @uri = URI.parse(service_url)
  end

  def method_missing(name, *args)
    post_body = { 'method' =&gt; name, 'params' =&gt; args, 'id' =&gt; 'jsonrpc' }.to_json
    resp = JSON.parse( http_post_request(post_body) )
    raise JSONRPCError, resp['error'] if resp['error']
    resp['result']
  end

  def http_post_request(post_body)
    http    = Net::HTTP.new(@uri.host, @uri.port)
    request = Net::HTTP::Post.new(@uri.request_uri)
    request.basic_auth @uri.user, @uri.password
    request.content_type = 'application/json'
    request.body = post_body
    http.request(request).body
  end

  class JSONRPCError &lt; RuntimeError; end
end

if $0 == __FILE__
  h = BitcoinRPC.new('http://rpcuser:rpcpassword@127.0.0.1:8332')
  p h.getbalance
  p h.getinfo
  p h.getnewaddress
  p h.dumpprivkey( h.getnewaddress )
end
</code></pre>

<p>But when I try running the file, I get a RPC.rb: end of file reached (EOFError)</p>

<p>I have the bitcoind running, I can run various commands, but I an not seem to get this script running.</p>

<bitcoind><client><ruby>
Ruby bitcoind JSON RPCCall
Bryan Singh
# Ruby bitcoind JSON RPCCall

(Asked by: username_0 on 2017-06-02)

I am trying the JSON-RPC call from <https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)#Ruby>

```
require 'net/http'
require 'uri'
require 'json'

class BitcoinRPC
  def initialize(service_url)
    @uri = URI.parse(service_url)
  end

  def method_missing(name, *args)
    post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
    resp = JSON.parse( http_post_request(post_body) )
    raise JSONRPCError, resp['error'] if resp['error']
    resp['result']
  end

  def http_post_request(post_body)
    http    = Net::HTTP.new(@uri.host, @uri.port)
    request = Net::HTTP::Post.new(@uri.request_uri)
    request.basic_auth @uri.user, @uri.password
    request.content_type = 'application/json'
    request.body = post_body
    http.request(request).body
  end

  class JSONRPCError < RuntimeError; end
end

if $0 == __FILE__
  h = BitcoinRPC.new('http://rpcuser:rpcpassword@127.0.0.1:8332')
  p h.getbalance
  p h.getinfo
  p h.getnewaddress
  p h.dumpprivkey( h.getnewaddress )
end

```

But when I try running the file, I get a RPC.rb: end of file reached (EOFError)

I have the bitcoind running, I can run various commands, but I an not seem to get this script running.

**Tags:** bitcoind, client, ruby


---

## 2 answers

### Answer by username_1 on 2017-11-02. Score: 1

The code is free of syntax errors, but it looks like you need to replace the `rpcuser` and `rpcpassword` in this line:

`h = BitcoinRPC.new('http://rpcuser:rpcpassword@127.0.0.1:8332')`

To the actual rpcuser and rpcpassword values that are set in your `bitcoin.conf` file.

If that doesn't work, I'd suggest doing some debugging by opening an `irb` session and running this code to see the result of the HTTP request to the RPC service:

```
require 'net/http'
require 'json'

post_body = { method: 'getblockchaininfo', params: [], id: 'jsonrpc' }.to_json

uri = URI.parse "http://rpcuser:rpcpassword@127.0.0.1:8332"

def http_post_request(uri, post_body)
  http    = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth uri.user, uri.password
  request.content_type = 'application/json'
  request.body = post_body
  http.request(request).body
end

http_post_request(uri, post_body)

```


---

### Answer by username_2 on 2017-06-03. Score: 0

I don't really know Ruby but these two lines looks off to me.

```
  class JSONRPCError < RuntimeError; end
end

```

Two `end`s?


---

