Perl与RESTful API交互的方法

使用LWP::UserAgent模块

LWP::UserAgent是Perl中常用的HTTP客户端模块,适合简单的RESTful API请求。
安装方法:
cpanm LWP::UserAgent

发起GET请求示例:

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = $ua->get('https://api.example.com/data');
die $response->status_line unless $response->is_success;
print $response->decoded_content;

发起POST请求示例:

my $response = $ua->post(
    'https://api.example.com/create',
    Content_Type => 'application/json',
    Content => '{"key":"value"}'
);

使用HTTP::Tiny模块

HTTP::Tiny是轻量级HTTP客户端,适合基础需求。
安装方法:
cpanm HTTP::Tiny

示例代码:

use HTTP::Tiny;
my $response = HTTP::Tiny->new->get('https://api.example.com/data');
die "$response->{status} $response->{reason}" unless $response->{success};
print $response->{content};

使用Mojo::UserAgent模块

Mojo::UserAgent来自Mojolicious框架,支持异步请求和高级功能。
安装方法:
cpanm Mojolicious

异步GET请求示例:

use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
$ua->get('https://api.example.com/data' => sub {
    my ($ua, $tx) = @_;
    die $tx->error if $tx->error;
    print $tx->res->body;
});
Mojo::IOLoop->start;

处理JSON数据

RESTful API通常返回JSON格式数据,需使用JSON模块解析。
安装方法:
cpanm JSON

解析示例:

use JSON;
my $data = decode_json($response->content);
print $data->{key};

添加请求头

部分API需要认证头或其他自定义头:

my $response = $ua->get(
    'https://api.example.com/protected',
    Authorization => 'Bearer token123'
);

错误处理

需检查HTTP状态码和响应内容:

unless ($response->is_success) {
    warn "API Error: " . $response->status_line;
    my $error = eval { decode_json($response->content) };
    warn $error->{message} if $error;
}

超时设置

避免请求长时间阻塞:

$ua->timeout(10);  # 设置10秒超时

Perl与RESTful API交互的方法

使用LWP::UserAgent模块

LWP::UserAgent是Perl中常用的HTTP客户端模块,适合简单的RESTful API请求。
安装方法:
cpanm LWP::UserAgent

发起GET请求示例:

use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = $ua->get('https://api.example.com/data');
die $response->status_line unless $response->is_success;
print $response->decoded_content;

发起POST请求示例:

my $response = $ua->post(
    'https://api.example.com/create',
    Content_Type => 'application/json',
    Content => '{"key":"value"}'
);

使用HTTP::Tiny模块

HTTP::Tiny是轻量级HTTP客户端,适合基础需求。
安装方法:
cpanm HTTP::Tiny

示例代码:

use HTTP::Tiny;
my $response = HTTP::Tiny->new->get('https://api.example.com/data');
die "$response->{status} $response->{reason}" unless $response->{success};
print $response->{content};

使用Mojo::UserAgent模块

Mojo::UserAgent来自Mojolicious框架,支持异步请求和高级功能。
安装方法:
cpanm Mojolicious

异步GET请求示例:

use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
$ua->get('https://api.example.com/data' => sub {
    my ($ua, $tx) = @_;
    die $tx->error if $tx->error;
    print $tx->res->body;
});
Mojo::IOLoop->start;

处理JSON数据

RESTful API通常返回JSON格式数据,需使用JSON模块解析。
安装方法:
cpanm JSON

解析示例:

use JSON;
my $data = decode_json($response->content);
print $data->{key};

添加请求头

部分API需要认证头或其他自定义头:

my $response = $ua->get(
    'https://api.example.com/protected',
    Authorization => 'Bearer token123'
);

错误处理

需检查HTTP状态码和响应内容:

unless ($response->is_success) {
    warn "API Error: " . $response->status_line;
    my $error = eval { decode_json($response->content) };
    warn $error->{message} if $error;
}

超时设置

避免请求长时间阻塞:

$ua->timeout(10);  # 设置10秒超时

Logo

更多推荐