The credentials method comes straight from LWP::UserAgent (since WWW::Mechanize is a subclass). It takes four arguments: the base, realm, user, and password. You don't have the realm in there, so the user name becomes the realm, and so on.

"Normal" browsers do this in two steps: they try to fetch the resource and get a 401 response that contains the challenge (which has the realm information). They ask the user for the name and password, which they then use to request the resource again. They save that information for future accesses to the same realm. The realm helps the user-agent keep things straight on the user side, and the server doesn't really care about it otherwise. Sometimes you'll need to pull the auth method from the challenge, but usually it's just "Basic".

If you want to do it in one step (so you don't get the 401 at all), you stick the "Authorization" header in the initial request. It makes no difference to the server that you don't know the realm name. With HTTP::Request you just add the header to the request. In WWW::Mechanize, you can use the add_header() method. The HTTP specification (RFC 2616) explains the format of the Authorization header.

--
brian d foy <bdfoy@cpan.org>

In reply to Re: WWW:::Mechanize and credentials by brian_d_foy
in thread WWW:::Mechanize and credentials by Sifmole

Title:
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, details, 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, summary, 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.