2

I just want to validate the website link whether it is connecting or not. I added the website also in the code. Please show some light on this.

Here is my code:

use Mojo::UserAgent;
my $ua  = Mojo::UserAgent->new;
my $timeout = $ua->request_timeout;
$ua = $ua->request_timeout(10);
my $res = $ua->get('https://www.aba.com')->result;

if    ($res->is_success)  { print 'Success' }
elsif ($res->is_error)    { print 'Failed ' . $res->message }
elsif ($res->code == 301) { print 'Redirect Success ' . $res->headers->location }
else                      { print 'Manual Check Required URL...' }

The above code is giving the following failed message:

Failed Service Temporarily Unavailable

1 Answers1

1

Anyhow, I resolved the failed issue, it may be useful to someone else:

use Mojo::Promise;
use Mojo::UserAgent;

my @urls = ('https://www.aba.com');

my $ua = Mojo::UserAgent->new;
my $res;
my @gets = map {
    my $url = $_;
    $res = $ua->get_p( $url )->then(
        sub { print "Success -- valid -- $url" },
        sub { print "Failed -- $url" },
        );
    } @urls;

Mojo::Promise->all( @gets )->wait;

The result is:

Success -- valid -- https://www.aba.com
  • Did you really solve it? The `get` can succeed (you get a response of any sort) but that response can indicate an error. The promise would fail if you couldn't make the request at all. – brian d foy Jul 06 '22 at 10:00
  • My question is url is valid or not. this code resolves it. I don't want to extract any data from the website. – Rajeshkanna Purushothaman Jul 06 '22 at 10:20
  • What does valid mean though? If you get a 404, is that still valid? That `is_error` in your previous code checks if the response is 4xx or 5xx. You now drop that check. If you drop that check in your original code, you will get the same result. The Promise isn't doing anything for you here. – brian d foy Jul 06 '22 at 18:34
  • Yes, your point of view is correct only. I need to validate the response code as well. I will verify and tweak it. – Rajeshkanna Purushothaman Jul 07 '22 at 10:29