<p>Use the following method to call BitcoinRPC using Ruby:</p>

<p>Create ruby file <code>test.rb</code> as</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://username:password@127.0.0.1:8332')
  txid= "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
  p h.getrawtransaction(txid)
end
</code></pre>

<p>NOTE: 
<code>username</code> and <code>password</code> are from your <code>bitcoin.conf</code> file.</p>

CC BY-SA 3.0
2018-02-27T00:21:38.537Z
mark this as answer if you run successful.
m2pathan
CC BY-SA 3.0
2018-02-22T07:31:42.563Z
2018-02-22T07:31:42.563Z
m2pathan
<p>I am getting the following error while trying RPC call from ruby. <code>bitcoind</code> server is running. I am using <code>bitcoin-ruby</code> library .</p>

<p>Code Tried in irb to test:</p>

<pre><code>require 'bitcoin'
=&gt; true
irb(main):002:0&gt; txid= "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
=&gt; "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
irb(main):003:0&gt; rtx= bitcoinRPC('getrawtransaction',[txid])
</code></pre>

<p>Error :</p>

<pre><code>NoMethodError: undefined method `bitcoinRPC' for main:Object
    from (irb):3
    from /usr/bin/irb:11:in `&lt;main&gt;'
</code></pre>

<p>Any suggetion will be highly appreciated.</p>

<p>Thanks in advance,</p>

<p>R</p>

<transactions><bitcoind><ruby>
Getting error while accessing bitcoin client from ruby using Rpc
Rubz
# Getting error while accessing bitcoin client from ruby using Rpc

(Asked by: username_0 on 2018-01-19)

I am getting the following error while trying RPC call from ruby. bitcoind server is running. I am using bitcoin-ruby library .
Code Tried in irb to test:
require 'bitcoin'
=> true
irb(main):002:0> txid= "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
=> "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
irb(main):003:0> rtx= bitcoinRPC('getrawtransaction',[txid])

Error :
NoMethodError: undefined method `bitcoinRPC' for main:Object
    from (irb):3
    from /usr/bin/irb:11:in `'

Any suggetion will be highly appreciated.
Thanks in advance,
R

**Tags:** transactions, bitcoind, ruby


---

## 1 answers

### Answer by username_1 on 2018-02-22. Score: 0 | Accepted answer

Use the following method to call BitcoinRPC using Ruby:
Create ruby file test.rb as
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://username:password@127.0.0.1:8332')
  txid= "611b40973fe68cc42b70ae5af365a449af458d76086415c6fa6c45364c36278e"
  p h.getrawtransaction(txid)
end

NOTE: 
username and password are from your bitcoin.conf file.

> **Comments:**
> **username_1**: mark this as answer if you run successful.  

---

