Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
(from comp.lang.perl.modules)

As usual, there's more than one way to do it.

1. Use the credentials method of the LWP::UserAgent object:

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

Eg: Say your URL is and when you try this with your browser, it pops up a box saying 'Enter username and password for XYZ'.

$netloc in this example is '' and $realm in this case is 'XYZ'.

2. Call the authorization_basic method of your HTTP::Request object before passing it to the LWP::UserAgent's request method:

$request->authorization_basic($uname, $pass);

If you use method 1, the LWP::UserAgent will send one request which the server will reject with a 401 error. One of the headers in the 401 response will be the realm. The LWP::UserAgent will then look up it's internal table (populated by your calls to credentials) to find a matching netloc and realm. If this lookup returns a username and password, the LWP::UserAgent will resend the request with a basic authorization header.

The second method allows you to set the username and password before making the initial request - assuming you supply the right credentials there will be no 401 error. There is no need to specify netloc and realm, as they are only used by the LWP::UserAgent's lookup routine.

'perldoc LWP::UserAgent' will tell you more about 1

'perldoc HTTP::Message' will tell you more about 2 (HTTP::Request is derived from HTTP::Message).


In reply to Re: LWP Authorization Problem by twerq
in thread LWP Authorization Problem by gr0f

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others imbibing at the Monastery: (2)
    As of 2024-07-19 05:17 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.