16

I created a Perl script to run an https task. When I run it I get the error LWP::Protocol::https not installed.

I cannot figure out, or find a tutorial or command on how exactly to install LWP::Protocol::http. Anyone have any idea how to install it? Installing LWP was quite easy.

I have installed LWP and installed Crypt-SSLeay, however I still get the error. Here is my code:

use LWP::UserAgent;

my $ua = LWP::UserAgent->new;


# set custom HTTP request header fields

my $req = HTTP::Request->new(PUT => "https://thesite.com");

$req->header('Authorization' => 'myauth');
$req->header('Accept' => 'JSON:Application/JSON');
$req->header('Content-Type' => 'JSON:Application/JSON');
$req->header('Host' => 'api.thesite.com');

$req->content('Text' => 'thetext');



my $resp = $ua->request($req);
if ($resp->is_success) {
    my $message = $resp->decoded_content;
    print "Received reply: $message\n";
}
else {
    print "HTTP POST error code: ", $resp->code, "\n";
    print "HTTP POST error message: ", $resp->message, "\n";
}
brian d foy
  • 129,424
  • 31
  • 207
  • 592
CRAIG
  • 977
  • 2
  • 11
  • 25

4 Answers4

23

sudo yum install perl-LWP-Protocol-https fixed issue for me.

12

Running sudo cpan install LWP::Protocol::https fixed this for me.

FCTW
  • 225
  • 3
  • 10
  • 1
    `sudo perl -MCPAN -we 'install "LWP::Protocol::https"'` didn't worked because some test failed, and your command worked. THANK YOU SO MUCH! – yannis Feb 17 '20 at 17:27
  • Note that the "install" in that command is a no-op. Just `cpan LWP::Protocol::https` is what you need. – brian d foy Oct 11 '21 at 13:18
0

If you get this error:

Can't locate LWP/Protocol/https.pm in @INC (@INC contains: /etc/cxs /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at (eval 5) line 2.

You need to install LWP::Protocol::https, using for instance:

cpan LWP::Protocol::https
brian d foy
  • 129,424
  • 31
  • 207
  • 592
0

A long time ago, LWP came with HTTPS support. Then, in 6.02 (in 2011), they unbundled the LWP::Protocol::https. If your program was set up on an ancient system and you are upgraded, this change can trip you up.

Since Perl does not have SSL support out of the box, it didn't make sense to have a protocol helper that was missing what it needs (and remember HTTPS everywhere wasn't quite as prevalent then). As a separate module, it's easier to get everything set up.

Now, here's the trick. LWP::Protocol::https needs IO::Socket::SSL which needs Net::SSLeay which needs openssl. How you install that is up to you, and if your platform has a ready-to-go package, that might be best (because if you know you don't want that, you're unlikely to ask your question).

If you want to do the whole chain yourself, start with openssl. Once that's sorted, you can install the module you need and the rest of the pre-requisites will take care of themselves:

# ... install openssl, set env vars if in a nonstandard location
% cpan LWP::Protocol::https

As a side note, older Perl practices didn't include using all the modules you knew you depended on. In this case, you formerly didn't declare a dependency on LWP::Protocol::https because you knew it came with LWP. Now it's much better to explicitly declare every thing you need even if you know some modules come in the same distribution. Maybe some time in the future they'll be in different distributions.

Although I use Mojo::UserAgent for all this now, I'd add the explicit dependency even though LWP takes care of it all internally. This makes static analysis a bit easier:

use LWP;
use LWP::Protocol::https;
brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • Is there a chance to install LWP 6.01 or earler (version before unbundling the LWP::Protocol::https from LWP distribution)? – AntonioK Jul 01 '23 at 14:26
  • libwww-perl-6.01 is still available for download: https://metacpan.org/release/GAAS/libwww-perl-6.01 – AntonioK Jul 01 '23 at 14:34