issue with CURLOPT_URL format in PERL - perl

i'm trying to implement some REST API functionality in PERL using WWW::Curl::Easy.
I suffer on curl error Code 3: "URL using bad/illegal format or missing URL No URL set!
While debugging this issue it seems that there is some issue with setting the URL CURLOPT.
For Example: IMHO this testcode should return the URL http://www.example.com
#!/usr/bin/perl
use strict;
use WWW::Curl::Easy;
my $curl = WWW::Curl::Easy->new;
$curl->setopt("WWW::Curl::Easy::CURLOPT_URL", 'http://www.example.com');
print $curl->getinfo("WWW::Curl::Easy::CURLINFO_EFFECTIVE_URL");
but i'm getting "43" as output.
Is there a bug in my module?
CURL WWW::Curl::Easy::Version 4.17
perl Version v5.16.2
System OSX 10.9

What are you using for documentation?
The following is how the WWW::Curl docs for WWW::Curl::Easy demonstrate getting started:
use strict;
use warnings;
use WWW::Curl::Easy;
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, 'http://example.com');

Related

Unable to get page via HTTPS with LWP::Simple in Perl

I try to download a page from an HTTPS URL with Perl:
use LWP::Simple;
my $url = 'https://www.ferc.gov/xml/whats-new.xml';
my $content = get $url or die "Unable to get $url\n";
print $content;
There seems to be a problem. Just can't figure out the error. I can't get the page. Is the get request improperly coded? Do I need to use a user agent?
LWP::Protocol::https is needed to make HTTPS requests with LWP. It needs to be installed separately from the rest of LWP. It looks like you installed LWP, but not LWP::Protocol::https, so simply install it now.

Perl url encoding using Curl

I am having an issue where I am using cURL inside a perl script to execute a http request. I believe my issue is related to special characters in the URL string but I cannot figure out how to make it work.
I can confirm that the URL is correct as I can run it from my browser.
My perl script is
#!/usr/bin/perl
use strict;
use warnings;
$url = "http://machine/callResync?start=2017-02-01 00.00.00.000&end=2017-02-01 23.23.999";
system "curl $url
It fails when it reaches the first whitespace. I tired to escape that using %20.
After that I put in %26 to escape the & but then I get another issue. I have tired a number of different combinations but it keeps failing.
Any idea's.
Use the URI module to correctly build a URL, and rather than shelling out to cURL you should use a Perl library like LWP::Simple to access the page
The disadvantage of LWP::Simple is that it may be too simple in that it provides no diagnostics if the transaction fails. If you find you need something more elaborate then you should look at
HTTP::Tiny,
LWP::UserAgent, or
Mojo::UserAgent.
If you need help with these then please ask
use strict;
use warnings 'all';
use URI;
use LWP::Simple 'get';
my $url = URI->new('http://machine/callResync');
$url->query_form(
start => '2017-02-01 00.00.00.000',
end => '2017-02-01 23.23.999',
);
my $content = get($url) or die "Failed to access URL";
Problem number 1: You used an invalid URL. Spaces can't appear in URLs.
my $url = "http://machine/callResync?start=2017-02-01%2000.00.00.000&end=2017-02-01%2023.23.999";
Problem number 2: Shell injection error. You didn't correctly form your shell command.
system('curl', $url);
or
use String::ShellQuote qw( shell_quote );
my $cmd = shell_quote('curl', $url);
system($cmd);

Perl mechanize response is only "<HTML></HTML>" with https

I'm kind of new with perl, even newer to Mechanize. So far, when I tried to fetch a site via http, it's no problem.
Now I need to fetch a site with https. I've installed Crypt::SSLeay via PPM.
When I use $mech->get($url), though, this is the only response I get:
"<HTML></HTML>"
I checked the status and success, both were OK (200 and 1).
Here's my code:
use strict;
use warnings;
use WWW::Mechanize;
use Crypt::SSLeay;
$ENV{HTTPS_PROXY} = 'http://username:pw#host:port';
//I have the https_proxy env variable set globally too.
my $url = 'https://google.com';
//Every https site has the same response,
//so I don't think google would cause problems.
my $mech = WWW::Mechanize->new(noproxy => 0);
$mech->get($url) or die "Couldn't load page";
print "Content:\n".$mech->response()->content()."\n\n";
As you can see I'm behind a proxy. I tried setting
$mech->proxy($myproxy);
but for no avail. I even tried to fetch it into a file, but when I checked it, I got the same response content.
Any kind of advice would be appreciated, since I'm just a beginner and there is still a lot to learn of everything. Thanks!
I think the answer lies here: How do I force LWP to use Crypt::SSLeay for HTTPS requests?
use Net::SSL (); # From Crypt-SSLeay
BEGIN {
$Net::HTTPS::SSL_SOCKET_CLASS = "Net::SSL"; # Force use of Net::SSL
$ENV{HTTPS_PROXY} = 'http://10.0.3.1:3128'; #your proxy!
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
}

Perl / Unix download pdf from website

post #2 :) don't worry I don't intend to count them all...
Is there an easy way to download a pdf file from a website using a perl or shell script?
If I have an url as such:
http://www.cs.middlebury.edu/~briggs/Courses/CS201-F12/js/js.pdf
Actually, i will have a cron job that will be running daily to download a pdf file from a website
any help?
Thanks
Look at wget or curl. Example: wget <URL> -O <output file>
The LWP set of modules has a cut-down version LWP::Simple which allows this sort of thing to be done very simply.
use strict;
use warnings;
use LWP::Simple 'getstore';
my $resp = getstore('http://www.cs.middlebury.edu/~briggs/Courses/CS201-F12/js/js.pdf', 'js.pdf');
print $resp, "\n";
The value of $resp is the HTTP status code and should normally be 200 for a successful operation.

Perl: how to fix SOAP::Lite delcampe-api-client example?

Here http://code.google.com/p/delcampe-api-client/wiki/Info#Perl is code example of delcampe-api-client in Perl. Obviously, there is an syntax error, running the code i got:
syntax error at test.pl line 9, near "-> service"
Bareword "SOAP::Lite" not allowed while "strict subs" in use at test.pl line 8.
Execution of test.pl aborted due to compilation errors.
If i change code to little bit more meaningful for me, like this:
#!/usr/bin/perl
use SOAP::Lite;
use SOAP::WSDL;
use strict;
use warnings;
my $service = new SOAP::Lite;
print $service
->uri('http://api.delcampe.net/soap.php?wsdl')
->getServerTime()
->result;
I got:
A service address has not been specified either by using SOAP::Lite->proxy() or a service description)
What is wrong with this code? What proxy? Service description?
[ If you think, i have no experience with SOAP, you are certainly right. Still, how to get this little example to work. PHP example worked nicely, but not the Perl's one. ]
If I were you, I would give "The Fine Documentation" a try. This should work, however:
#!/usr/bin/perl
use SOAP::Lite;
use SOAP::WSDL;
use strict;
use warnings;
my $service = SOAP::Lite->service('http://api.delcampe.net/soap.php?wsdl');
print $service->getServerTime()->result;
Edit:
Documentation can be found at http://guide.soaplite.com/ - also perldoc SOAP::Lite from the command line.
The example (above) on the web site appears not to work on more than one count: this one, however, is tested and works on my machine:
#!/usr/bin/perl
use SOAP::Lite;
use strict;
use warnings;
my $service = SOAP::Lite->service('http://api.delcampe.net/soap.php?wsdl');
print $service->getServerTime();