Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: davout on October 20, 2010, 02:12:15 PM



Title: Unable to connect using JSON-RPC
Post by: davout on October 20, 2010, 02:12:15 PM
Hello all,

I'd really be thankful for some help in getting my JSON RPC client to actually get to talk to my bitcoin daemon.
Basically, I've been actively trying different JSON client with pretty much the same results... 401 Unauthorized...

I've also tried the Curl based example from the docs which is working as a charm, so I'm really wondering wth i'm doing wrong...

Nothing helpful found in the debug.log (I can re-symlink it to /dev/null now :D )

Would anyone be kind enough to have a little look at my code and/or give me a couple of pointers ?

Thank you very much





irb(main):009:0> client = JsonRPC::Client.new("http://admin:passsword@localhost:8332/")
=> #<JsonRPC::Client:0x7ffe29900978 @address=#<Addressable::URI:0x3fff14c8028c URI:http://admin:passsword@localhost:8332/>>
irb(main):010:0> client.request 'getbalance', {}
JSON::ParserError: 705: unexpected token at '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML>
<HEAD>
<TITLE>Error</TITLE>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
</HEAD>
<BODY><H1>401 Unauthorized.</H1></BODY>
</HTML>
'
        from /usr/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `parse'
        from /usr/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `parse'
        from /usr/lib/ruby/gems/1.8/gems/jsonrpc-0.9.2/lib/jsonrpc.rb:20:in `request'
        from /usr/lib/ruby/1.8/net/http.rb:543:in `start'
        from /usr/lib/ruby/1.8/net/http.rb:440:in `start'
        from /usr/lib/ruby/gems/1.8/gems/jsonrpc-0.9.2/lib/jsonrpc.rb:19:in `request'
        from (irb):10



Contents of my bitcoin.conf
rpcpassword=password
rpcuser=admin


Title: Re: Unable to connect using JSON-RPC
Post by: Gavin Andresen on October 20, 2010, 07:16:30 PM
Ruby (or your Ruby JSON-RPC library) isn't setting the Authorization: HTTP header from the username:password in the URL.

I don't know anything about Ruby, but Mr. Google might be able to shed some light-- you need to either set the Authorization: header yourself to do the HTTP Basic authentication (I seem to recall some Ruby code on the Wikipedia page about HTTP Basic Authentication) or, in some languages/frameworks there's a way of setting up a 'context' for HTTP connections so the authentication happens properly.


Title: Re: Unable to connect using JSON-RPC
Post by: davout on October 20, 2010, 10:55:08 PM
Fixed, there were a couple of things wrong, one of them being effectively lame credentials handling.
Thanks for the pointer.

Will publish a ruby gem pretty soon with a Json RPC client and some more useful bitcoin related stuff.

Until then if someone is interested in the actual code : http://github.com/davout/bitcoin-bank.git