Take the 2-minute tour ×

I'm trying to access a protected file. Server is using digest authentication - which I can see from the printed out response. Here is the sample code:

use LWP;
use strict;

my $username = 'scott';
my $password = 'tiger';


print $response->content;

Name of the realm I got it from the popup window I get when I try to access that resource from the browser. Same username and password are working extremely fine in the browser and I'm able to see the content but when I run the above script it always says 401 Authorization required.

How does LWP work?

Do I need to ask LWP to send MD5 hash (digest) of the username and password or is it like internally it checks which authentication to use and sends the corresponding (basic/digest) way of sending credentials. My questions are

  1. How can I set LWP so that it sends digest of username and password?
  2. What if the server is using windows NTLM authentication protocol? How should I go about in such a situation?

any quick help is highly appreciated !

share|improve this question
Try to remove port number from "http://somesite.com:80". –  Ivan Nevostruev Nov 25 '09 at 18:54
It's not port but http:// should be removed - thanks Ivan –  Ram Nov 26 '09 at 5:10
add comment

3 Answers

up vote 13 down vote accepted

Consider the following excerpt from the LWP::UserAgent module's documentation:

$ua->credentials( $netloc, $realm )







share|improve this answer
Thanks....gbacon... Removing "http://" in credentials host solved the problem. Thank you so much. Anyway we are saying port no 80 so we need not say http I guess –  Ram Nov 26 '09 at 4:37
You're welcome! –  Greg Bacon Nov 26 '09 at 13:41
add comment

When you have these sorts of issues, use an HTTP sniffer to watch the transaction so you can see the headers your program is sending. In this case, you're probably not sending the credentials at all since the HTTP status is 401 instead of 403. That usually means you've made a mistake with the credentials, as gbacon notes in his answer.

share|improve this answer
add comment

HTTP GET Authed Request can also be done as follows

use LWP::UserAgent;

my $address = "localhost";
my $port = "8080";
my $username = "admin";
my $pass = "password";

my $browser = LWP::UserAgent->new;
my $req =  HTTP::Request->new( GET => "http://$address:$port/path");
$req->authorization_basic( "$username", "$pass" );
my $page = $browser->request( $req );
share|improve this answer
add comment

Your Answer


