我正在尝试用于Mojo::UserAgent验证应用程序的 gzip 压缩(内容编码)。
不幸的是,这个 UA 似乎默默地解码了内容并删除了 Content-Encoding 标头后记。
以下是我的最小示例
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 3;
use Mojo::UserAgent; # Version 8.26
my $ua = Mojo::UserAgent->new();
# As documented: https://docs.mojolicious.org/Mojolicious/Guides/Cookbook#Decorating-follow-up-requests
$ua->once(
start => sub {
my ( $ua, $tx ) = @_;
$tx->req->headers->header( 'Accept-Encoding' => 'gzip' );
}
);
my $tx = $ua->get('https://www.mojolicious.org');
is( $tx->req->headers->header('Accept-Encoding'), 'gzip', qq{Request Accept-Encoding is "gzip"} );
ok( $tx->res->is_success, "Response is success" );
# The following assertion fails.
# My theory …Run Code Online (Sandbox Code Playgroud) 我想知道下面的东西是否可以用Mojo :: UserAgent做:
假设我有以下代码:
my $ua = Mojo::UserAgent->new;
my $res = $ua->get('mojolicious.org/perldoc')->result;
Run Code Online (Sandbox Code Playgroud)
是否有可能拦截Mojo :: UserAgent请求并将其发送到知道javascript的其他Web客户端,其结果发送回Mojo :: Transaction :: HTTP($ res above),其中用户可以使用Mojo :: UserAgent界面结果.
即我想要以下内容:
Mojo :: UserAgent-> HTTP请求 - >拦截HTTP请求 - >发送HTTP请求到Web客户端支持javascript,如WWW :: Chrome :: Mechanize或FireFox :: Marionette - > JavaScript Web Client请求 - >返回的结果被拦截并更改为Mojo :: Transaction :: HTTP
要么
Mojo :: UserAgent - >非阻塞HTTP请求 - >非阻塞HTTP共振 - >发送到webkit的嵌入式Web浏览器 - >获取结果为Mojo :: Transaction :: HTTP
任何想法/例子如何让Mojo :: UserAgent使用javascript?
我必须使用一些配置错误的Web服务器,所以我开始处理HTML元标记以将信息反馈回Web用户代理对象。我在Mojolicious中尝试了多种方法来完成此操作,并决定在响应中查找“完成”事件。我的目标是使其余代码几乎不可见,因此过程甚至都不知道这种情况在发生。
但是,由于我不能完全放下手指,这只是不适合我。除了中的特定代码外process_meta_options,还有其他Mojolicious方式可以做到这一点吗?例如,具有用户定义的回调的Mojo :: UserAgent get()使用该read事件,但我倾向于认为这可能会干扰事情。或者,我可能只是想得太多。
use v5.20;
use feature qw(signatures);
no warnings qw(experimental::signatures);
use Data::Dumper;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $tx = $ua->build_tx( GET => 'http://blogs.perl.org' );
$tx->res->on(
finish => \&process_meta_options
);
$tx = $ua->start( $tx );
say "At end, charset is ", $tx->res->content->charset;
sub process_meta_options ( $res ) {
$res
->dom
->find( 'head meta[charset]' ) # HTML 5
->map( sub {
my $content_type = $res->headers->header( 'Content-type' );
return unless …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 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;
Run Code Online (Sandbox Code Playgroud) 我试图从一个子例程返回一个Promise,该子例程包含从HTTP请求获得的一些数据到Web服务器。但是我无法要求then结果。缩小范围后,似乎不可能将从get_p中返回的承诺分配给变量,然后将其用作承诺。
这是一个例子。我本以为两个请求完全相同,但是只有第二个请求在then块中运行代码。
有人可以解释一下两者之间的区别是什么,如果我想在子程序then外链接更多方法,我应该如何从子程序返回promise ?
#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use 5.024;
use Data::Dumper;
use Mojo::IOLoop;
use Mojo::UserAgent;
my $promise = Mojo::UserAgent->new->get_p('http://example.com');
$promise->then(sub {
my $tx = shift;
warn 'Using variable';
warn $tx->result->body;
})->wait;
Mojo::UserAgent->new->get_p('http://example.com')
->then(sub {
my $tx = shift;
warn 'Not using variable';
warn $tx->result->body;
})->wait;
Run Code Online (Sandbox Code Playgroud) 如何在 mojo 响应中访问 JSON?
$txn = $ua->post( $url, $headers, json => {json} )
Run Code Online (Sandbox Code Playgroud)
从 txn 获取 JSON 响应的方法是什么?
我试图找出如何使用Mojo::DOMUTF8 (和其他格式......不仅仅是 UTF8)。它似乎搞乱了编码:
my $dom = Mojo::DOM->new($html);
$dom->find('script')->reverse->each(sub {
#print "$_->{id}\n";
$_->remove;
});
$dom->find('style')->reverse->each(sub {
#print "$_->{id}\n";
$_->remove;
});
$dom->find('script')->reverse->each(sub {
#print "$_->{id}\n";
$_->remove;
});
my $html = "$dom"; # pass back to $html, now we have cleaned it up...
Run Code Online (Sandbox Code Playgroud)
这是我在保存文件而不通过 Mojo 运行它时得到的结果:
...然后通过 Mojo 一次:
FWIW,我正在使用 , 抓取 HTML 文件Path::Tiny:
my $utf8 = path($_[0])->slurp_raw;
据我了解,应该已经将字符串解码为可供 Mojo 使用的字节?
更新:在布莱恩的建议之后,我研究了如何找出编码类型以正确解码它。我尝试了 Encode::Guess 和其他一些方法,但他们似乎在很多方面都出错了。这似乎可以解决问题:
my $enc_tmp = `encguess $_[0]`;
my ($fname,$type) = split /\s+/, $enc_tmp;
my $decoded …Run Code Online (Sandbox Code Playgroud) 我如何告诉我的 MojoliciousMojo::UserAgent仅使用 IPv4,这可能吗?
(就像是:wget --inet4-only https://blabli.com)
我需要这个的原因是,在我使用此代理的 Openshift Cluster 中,IPv6 不起作用。
我正在尝试使用非阻塞请求,
Mojo::UserAgent
但是当我运行下面的代码时,我得到了
在连接(.)或字符串中使用未初始化的值$ _
就print行了.
如何$_在回调中访问?
my $ua = Mojo::UserAgent->new();
my @ids = qw( id1 id2 id3 );
foreach ( @ids ) {
my $res = $ua->get('http://my_site/rest/id/'.$_.'.json' => sub {
my ($ua, $res) = @_;
print "$_ => " . $res->result->json('/net/id/desc'), "\n";
});
}
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Run Code Online (Sandbox Code Playgroud)