our $ua = LWP::UserAgent->new;
my $response = $ua->get($url);
if($response->is_success) {
my $perl_hash_or_arrayref = decode_json(encode("UTF-8", $response->decoded_content));
上面的代码将响应转换为 UTF-8 编码,而无需告诉它响应使用的编码。
我认为在某些情况下可能会失败。
如何明确告知LWP::UserAgent
响应编码?
答案 0 :(得分:2)
请参阅HTTP::Message
中的decoded_content
文档:
$ mess-> decoding_content(%options)
Returns the content with any Content-Encoding undone and for textual content the raw content encoded to Perl's Unicode strings. If the Content-Encoding or charset of the message is unknown this method will fail by returning undef. The following options can be specified. charset This override the charset parameter for text content. The value none can used to suppress decoding of the charset.