Why can't I set the proxy address in Mojo::UserAgent?

Viewed 225

For some reason I can not set proxy using Mojo::UserAgent

Here is a sample script:

use strict;
use warnings;
use feature 'say';
use Mojo::JSON qw(decode_json);
use Mojo::UserAgent;

sub _get_new_proxy {
    my $cmd = `docker run -it nrdhm/proxybroker proxybroker find --types HTTP -l 1 -f json`;
    my $res = decode_json $cmd;
    my $obj = $res->[0];
    return $obj;
}

sub _new_proxy_socket_str {
    my $hash = _get_new_proxy();
    return ( lc $hash->{types}[0]{type} ) . '://' . $hash->{host} . ':' . $hash->{port};
}

my $ua           = Mojo::UserAgent->new();
my $ip_check_url = 'https://api.ipify.org';

say "Current ip: " . $ua->get($ip_check_url)->res->text;

my $proxy_socket = _new_proxy_socket_str();
$ua->proxy->http($proxy_socket)->https($proxy_socket);
say "Set proxy:" . $proxy_socket;

say "Current ip: " . $ua->get($ip_check_url)->res->text;

Same feature works fine in LWP::UserAgent:

use strict;
use warnings;
use LWP::UserAgent ();
use JSON::MaybeXS;
use feature 'say';

sub _get_new_proxy {
    my $cmd = `docker run -it nrdhm/proxybroker proxybroker find --types HTTP -l 1 -f json`;
    my $res = decode_json $cmd;
    my $obj = $res->[0];
    return $obj;
}

sub _new_proxy_socket_str {
    my $hash = _get_new_proxy();
    return ( lc $hash->{types}[0]{type} ) . '://' . $hash->{host} . ':' . $hash->{port};
}

my $ua           = LWP::UserAgent->new( timeout => 10 );
my $ip_check_url = 'https://api.ipify.org';

say "Current ip: " . $ua->get($ip_check_url)->decoded_content;

my $proxy_socket = _new_proxy_socket_str();
$ua->proxy( [ 'http', 'https' ], $proxy_socket );
say "Set proxy: " . $proxy_socket;
say "Current ip: " . $ua->get($ip_check_url)->decoded_content;

Please help me to figure out what is the problem

UPD: Seems like problem is only with HTTPS requests. If I use http://api.ipify.org instead https://api.ipify.org Mojo::UserAgent works fine

0 Answers
Related