如何使用 Mojo::Useragent 放置文件?

How to PUT a file using Mojo::Useragent?

我正在尝试使用 PUT 方法通过 Mojo::UserAgent 上传文件,文件可能很大,而不是将文件内容作为标量传递,还有其他方法吗?

这是我试过的:

use strict;
use warnings;
use Mojo::UserAgent;
use Mojo::Asset::File;


my $ua = Mojo::UserAgent->new;

my $file = $ARGV[0];

die("File not found") unless(-f $file);

my $a_file = Mojo::Asset::File->new(path => $file);

my $tx = $ua->put('https://postman-echo.com/put' => {'X-Test' => '123G'} => $a_file);

print $tx->success;

print "\n\n";

print $tx->result->body;

print "\n\n";

print $tx->req->text;

请参阅 build_tx in Mojo::UserAgent 并注释示例

# PUT request with content streamed from file

tx in Mojo::UserAgent::Transactor

my $ua = Mojo::UserAgent->new;
my $put = $ua->build_tx(PUT => '…' => {'X-Test' => '123G'});
$put->req->content->asset(Mojo::Asset::File->new(path => $file));
my $tx = $ua->start($put);