Suggestions and discussion of future versions
coolstory
New User
Posts: 2 Joined: Tue Apr 02, 2013 2:49 pm
Post
by coolstory » Tue Apr 02, 2013 2:52 pm
I'm trying to interface with the json-api using perl without much success. I'm not sure what is being returned by the json-rpc request, but it's definitely confusing the perl library.
Any pointers would be appreciated!
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
use JSON::RPC::Client;
use Data::Dumper;
use constant {
HOST => 'localhost',
PORT => 2064,
PASS => 'password',
HTTP => 'http'
};
my $url = HTTP.'://'.HOST.':'.PORT.'/json';
my $client = new JSON::RPC::Client;
my $request = {
'method' => 'auth.login',
'params' => [ PASS ],
'id' => '2',
};
my $res = $client->call($url,$request);
if ($res){
if ($res->is_error) { print "Error : ", $res->error_message; }
else { print Dumper($res->result); }
} else {
print $client->status_line;
}
Cas
Top Bloke
Posts: 3681 Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland
Post
by Cas » Tue Apr 02, 2013 4:39 pm
The problem is possibly that the response is compressed, I am not sure whether it is possible with that module but I found that you can use LWP and this example works for me.
Code: Select all
#!/usr/bin/perl -w
use strict;
use JSON;
use LWP::Simple;
use HTTP::Cookies;
use constant {
HOST => 'localhost',
PORT => 8112,
PASS => 'deluge',
HTTP => 'http'
};
my $url = HTTP.'://'.HOST.':'.PORT.'/json';
my $data = {
'method' => 'auth.login',
'params' => [ PASS ],
'id' => '2',
};
my $json = to_json($data);
my $ua = LWP::UserAgent->new();
my $cookie_jar = HTTP::Cookies->new(
file => "cookies.lwp",
autosave => 1,
);
$ua->cookie_jar( $cookie_jar );
my $request = HTTP::Request->new(POST => $url);
$request->content_type('application/json');
$request->content($json);
my $res = $ua->request($request);
if ($res->is_success)
{
print "Succeeded\n";
print "Header:\n ". $res->dump . "\n";
print "Json:\n" . $res->decoded_content;
} else {
print "Failed\n";
}
Last edited by
Cas on Tue Apr 02, 2013 5:22 pm, edited 1 time in total.
Reason: Added saving to cookie_jar
Cas
Top Bloke
Posts: 3681 Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland
Post
by Cas » Tue Apr 02, 2013 5:14 pm
I just verified that it is the issue and the module could be hacked with these changes to force gzip decode:
Code: Select all
diff /usr/share/perl5/JSON/RPC/Client.pm.old /usr/share/perl5/JSON/RPC/Client.pm
112c112
< return unless($result->content); # notification?
---
> return unless($result->decoded_content); # notification?
186c186
< my $content = ( $json || JSON->new->utf8 )->decode( $obj->content );
---
> my $content = ( $json || JSON->new->utf8 )->decode( $obj->decoded_content );
189c189
< jsontext => $obj->content,
---
> jsontext => $obj->decoded_content,
Cas
Top Bloke
Posts: 3681 Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland
Post
by Cas » Tue Apr 02, 2013 5:24 pm
I have modified my script to include how to save the cookie and you can do the same with JSON-RPC module like this:
Code: Select all
use HTTP::Cookies;
my $cookie_jar = HTTP::Cookies->new(
file => "cookies.lwp",
autosave => 1,
);
$client->ua->cookie_jar( $cookie_jar );
coolstory
New User
Posts: 2 Joined: Tue Apr 02, 2013 2:49 pm
Post
by coolstory » Tue Apr 02, 2013 5:59 pm
Thank you! This should help immensely.
I want to integrate
https://github.com/autodl-irssi-community/autodl-irssi with deluge so it has the same support rtorrent currently enjoys. We'll see if I can get there.
Btw, if anyone is interested in doing this that actually knows perl/deluge... it would be much appreciated