Subject: | FIX: make JSONRPC::Transport::HTTP::Client capable of sending utf8 strings. |
In module JSONRPC::Transport::HTTP, package
JSONRPC::Transport::HTTP::Client, add 'use Encode;' and replace 'sub
send {' with this:
sub send {
require LWP::UserAgent;
my ($self, $content) = @_;
my ($url, $proxy_url) = @{$self->{_proxy}};
my $ua = LWP::UserAgent->new();
$ua->agent(ref($self) . "/$JSONRPC::VERSION " . $ua->agent());
$ua->proxy(['http','https'], $proxy_url) if($proxy_url);
my $content_type = 'text/plain'; # or 'application/x-
javascript' ?
my $is_utf8 = Encode::is_utf8($content);
if ($is_utf8) {
# Turn off utf8 flag else post fail with '500 Wide
character in syswrite', and Content-Length won't be correct.
# What would be nice here is some real javascript
unicode encoding e.g. '\u044d' for perl's "\x{44d}" character, etc.
Encode::_utf8_off($content);
$content_type .= '; charset=UTF8';
}
my $response = $ua->post(
$url,
'Content_Type' => $content_type,
'Content' => $content,
'Context_Length' => length($content),
);
return $response;
}
This is for Perl 5.8. I don't know if it will work with Perl 5.6.
Perhaps someone can verify that.
The work around until the bug above is fixed is to create a new client
class like this:
#!/usr/local/bin/perl -w
use strict;
use Encode;
use Data::Dumper qw(Dumper);
use JSONRPC::Transport::HTTP;
package JSONRPC::Custom;
use base qw(JSONRPC::Transport::HTTP);
1;
package JSONRPC::Custom::Client;
use base qw(JSONRPC::Transport::HTTP::Client);
sub send {
require LWP::UserAgent;
my ($self, $content) = @_;
my ($url, $proxy_url) = @{$self->{_proxy}};
my $ua = LWP::UserAgent->new();
$ua->agent(ref($self) . "/$JSONRPC::VERSION " . $ua->agent());
$ua->proxy(['http','https'], $proxy_url) if($proxy_url);
my $content_type = 'text/plain'; # or 'application/x-
javascript' ?
my $is_utf8 = Encode::is_utf8($content);
if ($is_utf8) {
Encode::_utf8_off($content);
$content_type .= '; charset=UTF8';
}
my $response = $ua->post(
$url,
'Content_Type' => $content_type,
'Content' => $content,
'Context_Length' => length($content),
);
return $response;
}
1;
package main;
my $uri = 'http://127.0.0.1/json-rpc.cgi';
my $call = JSONRPC::Custom
->proxy($uri)
->call('echo',["Reversed Euro \x{44D} symbol."]);
my $res = $call->result();
if ($res->error()) {
# Dirty hack to access HTTP::Response object:
print "Oops: " . $call->{_response}->status_line(), "\n";
}
else {
my $s = $res->result();
#print "$s\n";
print Dumper($s);
}