fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

hi How can I get either of these two modules to ask for authentication? My very simple code returns a '401 unauthorized'...is it possible that the browser pops up a username/password box and then reissues the request? Thanks in advance
1 use strict; 2 use warnings; 3 4 use LWP::UserAgent; 5 6 my $url='http://<>.com/justifier'; 7 my $ua = LWP::UserAgent->new; 8 $ua->credentials($url, 'test', 'user', 'pass'); 9 my $response = $ua->get($url); 10 $response->is_success or die $response->status_line; 11 print $response->decoded_content;

Replies are listed 'Best First'.
Re: lwp (mechanize) authentication
by Corion (Patriarch) on Jul 25, 2011 at 13:06 UTC

    I don't think there is a hook or callback that will make LWP::UserAgent ask your code to supply credentials. All credentials must be supplied before making the request.

    If your request fails even though you believe you have supplied the appropriate username and password, have you checked that the $netloc and $realm parameters are correct, and match what the server sends and what LWP::UserAgent expect? The LWP::UserAgent documentation says

    The $netloc is a string of the form "<host>:<port>". The username and password will only be passed to this server. Example:
    $ua->credentials("www.example.com:80", "Some Realm", "foo", "secret" +);
Re: lwp (mechanize) authentication
by Khen1950fx (Canon) on Jul 25, 2011 at 14:32 UTC
    You can use LWP::AuthenAgent to ask for authentication. Here's an example. The user is "guest", and the password is "guest":
    #!/usr/bin/perl use strict; use warnings; use LWP::AuthenAgent; my $url = 'http://jigsaw.w3.org/HTTP/Basic/'; my $ua = new LWP::AuthenAgent; my $response = $ua->request(new HTTP::Request 'GET' => $url); print $response->decoded_content, "\n";