简体   繁体   English

Curl 至 Perl HTTP 请求

[英]Curl to Perl HTTP Request

Guys I need this curl request be translated to LWP::UserAgent HTTP Request伙计们,我需要将此 curl 请求转换为 LWP::UserAgent HTTP 请求

echo 'test{test="test"} 3' | curl -v --data-binary @- http://localhost:9090/api/metrics

What I've tried is this:我试过的是这样的:

my $ua = LWP::UserAgent->new;
my $res = $ua->post('http://localhost:9090/api/metrics', ['test{test="test"}' => 3]);
die Dumper $res

But the response says但回应说

'_rc' => '400',
'_msg' => 'Bad Request',
'_content' => 'text format parsing error in line 1: unexpected end of input stream

You can try use the following POST request:您可以尝试使用以下POST请求:

use feature qw(say);
use strict;
use warnings;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new();
my $res = $ua->post('http://localhost:9090/api/metrics', Content => 'test{test="test"} 3');
if ($res->is_success) {
    say $res->decoded_content;
}
else {
    die $res->status_line;
}

And, since you didn't ask, here's a Mojo example:而且,既然你没有问,这里有一个 Mojo 示例:

use v5.10;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new();
my $tx = $ua->post(
    'http://httpbin.org/post',
    => 'test{test="test"} 3'
    );
if ($tx->result->is_success) {
    say $tx->result->body;
}
else {
    die $tx->result->code;
}

It's basically the same as LWP except that Mojo returns a transaction object so you can play with the request too.它与 LWP 基本相同,只是 Mojo 返回事务 object,因此您也可以处理该请求。 It's something I wanted in LWP even before Mojo existed.这是我在 LWP 中想要的东西,甚至在 Mojo 存在之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM