Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: jfreak53 on October 08, 2011, 09:51:41 PM



Title: Perl Client Problems
Post by: jfreak53 on October 08, 2011, 09:51:41 PM
Anyone have any experience developing a perl client with bitcoin and JSON?  Having a problem when sending parameters to the RPC.  When sending params it comes back "500 Internal Server Error", without params RPC works fine.

Have tried a bunch of way but this is how it should be, right?

Code:
my $obj = {
      method  => 'listreceivedbyaddress',
      params  => [ 0, true ],
   };

Keep getting that 500 error with params, like I said without params works fine.  Tried ', " and I don't know how many other combinations.  Anyone else ever have this problem?


Title: Re: Perl Client Problems
Post by: 2112 on October 08, 2011, 11:26:20 PM
Post the whole non-working but runnable code snippet. Otherwise it is hard to guess.

Edit: yeah, I'm slow. Change "true" to "JSON::true".


Title: Re: Perl Client Problems
Post by: jfreak53 on October 09, 2011, 12:27:39 AM
It's off the API page.

Code:
my $obj = {
      method  => 'listreceivedbyaddress',
      params  => [ 0, true ],
   };
 
  my $res = $client->call( $uri, $obj );
 
  if ($res) {
if ($res->is_error) { print "Error : ", $res->error_message; exit; }
else {
print Dumper($myres);
}
  } else {
        print $client->status_line;
exit;
  }

As stated it works without the params part just fine, but if I include the params in any form it hits the line:

Code:
print $client->status_line;
exit;

Because it doesn't work with params for some reason, well I figure we have them in wrong some how but not sure how.


Title: Re: Perl Client Problems
Post by: mndrix on October 09, 2011, 01:27:17 AM
Without seeing your entire program (for example, variables $client and $uri aren't defined in your snippet above), it's hard to say for sure what's going wrong.  I'm suspicious of "true" in the params line since Perl has no native boolean type.  In this context, you probably need $JSON::true

I use Finance::Bitcoin::API (http://search.cpan.org/perldoc?Finance::Bitcoin::API) for Perl programs that interact with the Bitcoin API.  It works great.  Here's an example:

Code:
use strict;
use warnings;
use Finance::Bitcoin::API;
use Data::Dumper;

my $uri = 'http://username:password@127.0.0.1:8332';
my $api = Finance::Bitcoin::API->new( endpoint => $uri );
my $received = $api->call( 'listreceivedbyaddress', 0, $JSON::true );
print Dumper($received);


Title: Re: Perl Client Problems
Post by: jfreak53 on October 09, 2011, 01:35:29 AM
Beautiful, that's actually what it was.  Didn't really think it would matter since it was passing it to the bitcoind program and it was handling the boolean.  But that worked thank you.


Title: Re: Perl Client Problems
Post by: jfreak53 on October 09, 2011, 02:19:11 PM
Ok now the same problem am having with the 'sendtoaddress' command.  The amount is the problem here.  Bitcoind is expecting a real constant, and it's giving the same error.

Have tried Data::Types and to_real, to_float and to_decimal, none seem to work.  Also tried just sending the amount, for instance, this doesn't work with any of the above or alone:

Code:
my $obj = {
      method  => 'sendtoaddress',
      params  => ["$in{address}", $in{amount}],
   };

But this does:

Code:
my $obj = {
      method  => 'sendtoaddress',
      params  => ["$in{address}", 0.005],
   };

Any other ideas on how to convert that amount to a real in perl??


Title: Re: Perl Client Problems
Post by: jfreak53 on October 09, 2011, 02:58:05 PM
Ok nevermind did a small trick and made it work, don't know if it's the right way but it works for future reference anyone looking.

Correct way is:

Code:
$amt = $in{amount} + 0.0;
  my $obj = {
      method  => 'sendtoaddress',
      params  => ["$in{address}", $amt],
   };


Title: Re: Perl Client Problems
Post by: ethought on May 31, 2013, 02:28:56 PM
Ok nevermind did a small trick and made it work, don't know if it's the right way but it works for future reference anyone looking.

Correct way is:

Code:
$amt = $in{amount} + 0.0;
  my $obj = {
      method  => 'sendtoaddress',
      params  => ["$in{address}", $amt],
   };

Thank you - just what I was looking for.