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

I'm stuck getting basic http authentication working right with POST and I've come to the monastery seeking wisdom and clarity... I can GET okay authenticated with this code: ----------------------------------------------
use LWP::UserAgent; $ua = new LWP::UserAgent; $req = new HTTP::Request GET => 'http://www.sn.no/secret/'; $req->authorization_basic('aas', 'mypassword'); print $ua->request($req)->as_string; # the above code returns the html code for the form
But I cannot POST the way it would seem I should be able too with this code: ----------------------------------------------
use LWP::UserAgent; $ua = new LWP::UserAgent; $ua->credentials('user', 'password'); my $req = HTTP::Request::Common::POST($URL, [ 'txtUserID' => $USERID, 'txtPassword' => $PASSWORD, 'txtHeader' => $header, 'txtReportArea' => $data, 'onbtnSubmitReportWebEvent(btnSubmitReport)' => 'Submit Report +' ]); $req->authorization_basic('aas', 'mypassword'); print $req->status_line, "\n"; # the above returns 401 Access Denied
I can manually login (basic authentication) to the site and POST data via the form, but I need to automate this...

Help?!

Replies are listed 'Best First'.
Re: http POST + authentication
by Mr. Muskrat (Canon) on Dec 30, 2002 at 18:02 UTC

    I could be wrong because I didn't try it but I think you mean:

    use HTTP::Request::Common; use LWP::UserAgent; $ua = new LWP::UserAgent; $ua->credentials('user', 'password'); my $req = new HTTP::Request::Common(POST $URL, # note how it is calle +d! [ 'txtUserID' => $USERID, 'txtPassword' => $PASSWORD, 'txtHeader' => $header, 'txtReportArea' => $data, 'onbtnSubmitReportWebEvent(btnSubmitReport)' => 'Submit Report +' ]); $req->authorization_basic('aas', 'mypassword'); print $req->status_line, "\n";

      In the docs of LWP::UserAgent I read:

      $ua->credentials($netloc, $realm, $uname, $pass)
      Set the user name and password to be used for a realm.

      So it seems that you need to furnish the realm (i.e. the name of the protected area of the website)and the "netloc" (whatever that may be).

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        I saw that looking through the libwww-perl mailing list archives, here's what they look like:
        $ua->credentials(< 'servername:portnumber', 'realm-name', 'username' => 'password' );
        NOTE: You have to specify the port number above!
      yep...I missed that too... need to declare that module.. you should also probably add the following to all your code:
      #!\perl\bin\perl -w use strict;
      It will save you lots of headaches and force you to code better. :0)

      You are wrong. LWP uses HTTP::Request::Common so you don't need to do this as it has already been imported.

Re: http POST + authentication
by JamesNC (Chaplain) on Dec 30, 2002 at 17:30 UTC
    I don't use LWP::, but I noticed that you did not create a new instance on line 4: for your $req could it be that it should be:
    my $req = new HTTP::Request::Common::POST($URL, [ # rest of your code here...
      Have you even looked at the POD for HTTP::Request::Common? HTTP::Request::Common::POST is not a package, it's a subroutine (exported by default, so that could also be my $req = POST($URL, ...); ). The very least you can do before posting such an assumption is looking at the POD on search.cpan.org.

      Makeshifts last the longest.

      my $request = new HTTP::Request::Common::POST("$URL",
      adding the "new" gives me this error:
      "Bareword found where operator expected at ./test.pl line 19, near "ne +w HTTP::Request::Common::POST" (Do you need to predeclare new?)"
      If I understand correctly, I have already created a new session with
      $ua = LWP::UserAgent->new;

        The "indirect object" syntax is source of many syntax problems. Rather omit it in order not to encounter such trouble.

        my $request = HTTP::Request::Common::POST->new("$URL", #...

        In the end, this is Perl and needn't look like Java or C++

        --
        http://fruiture.de