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

I am trying to use CURLOPT_USERPWD with LWP::Curl. I need to pass userid and password. I've attempted:

my $lwpcurl = LWP::Curl->new( CURLOPT_USERPWD => 'USER:password', );

But I am still receiving a 401 error. Is this supported by LWP::Curl? Do I need to use a different module? I need to do an https post and provide username and password. Thanks. Geoffrey

Replies are listed 'Best First'.
Re: LWP::Curl and CURLOPT_USERPWD
by Corion (Patriarch) on Jun 06, 2016 at 17:57 UTC

    Looking at the source code, it seems that LWP::Curl doesn't pass through all options to Net::Curl::Easy but only the ones it knows about.

    You should be able to set the additional options by using:

    $lwpcurl->{agent}->setopt( CURLOPT_USERPWD, "$user:$password" );

      Thanks, I tried that and got:

      Argument "CURLOPT_USERPWD" isn't numeric in subroutine entry at ./biphttppost.pl line 8 (#2) (W numeric) The indicated string was fed as an argument to an operator that expected a numeric value instead. If you're fortunate the message will identify which operator was so unfortunate.

        Most likely, you will need to import CURLOPT_USERPWD from Net::Curl or Net::Curl::Easy. In fact, Curl::LWP has this line at its top:

        use Net::Curl::Easy qw(:constants)

        Maybe by adding that same line to your code you get the values for CURLOPT_USERPWD imported into your code as well.

Re: LWP::Curl and CURLOPT_USERPWD
by NetWallah (Canon) on Jun 06, 2016 at 19:41 UTC
    For perl LWP, the more common way to login sets up authorization in the REQUEST:
    $req->authorization_basic($username, $password);
    "CURLOPT_USERPWD" seems to be a PHP option.

            This is not an optical illusion, it just looks like one.

      I don't see a reference to authorization_basic() for LWP::Curl. I'm doing a $lwpcurl->post() I assume the authentication would be a part of the post method.

      I had hoped I could include the username and password as curl will accept it with userid:password@http://... but it does not like that either.

        Hmm - you are right - looks like LWP::Curl does not implement an authentication mechanism. You may have to use plain LWP.

                This is not an optical illusion, it just looks like one.