Reputation: 117
#!/usr/bin/perl
use strict;
use warnings;
use JSON qw( decode_json );
use LWP::Simple;
my $cavirtex = get('https://www.cavirtex.com/api2/orderbook.json?currencypair=BTCCAD');
print $cavirtex;
When compiled, I get...
Use of uninitialized value $cavirtex in print at cavirtex.pl line 9.
More info: https://www.cavirtex.com/api_information#orderbook
Thanks.
EDIT/MORE INFO: In my program, when I replace...
https://www.cavirtex.com/api2/orderbook.json?currencypair=BTCCAD
with...
https://www.bitstamp.net/api/ticker/
it works just fine. Weird.
Upvotes: 2
Views: 889
Reputation: 13792
@Dave Cooper answer is great. You can also change the LWP::Simple user agent this way:
use strict;
use warnings;
use JSON qw( decode_json );
use LWP::Simple qw/$ua get/;
$ua->agent('Mozilla/5.0');
my $cavirtex = get('https://www.cavirtex.com/api2/orderbook.json?currencypair=BTCCAD');
print $cavirtex;
NOTE: The $ua
exported variable is an instance of LWP::UserAgent
. LWP::Simple
operations are delegated in this $ua
object
Upvotes: 2
Reputation: 10714
Running the following command:
perl -MLWP::Simple -e 'getprint "https://www.cavirtex.com/api2/orderbook.json?currencypair=BTCCAD"'
Yields the following result:
403 Forbidden <URL:https://www.cavirtex.com/api2/orderbook.json?currencypair=BTCCAD>
However, visiting the URL in a browser seems to work fine. This gives me the hunch that the user-agent is being checked or a cookie is required.
I decided to manually specify a known-good user-agent into my request. If this didn't work, I'd toy around with generating cookies.
Using the following code I got the desired results:
require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->agent("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
my $response = $ua->get('https://www.cavirtex.com/api2/orderbook.json?currencypair=BTCCAD');
if ($response->is_success) {
print $response->decoded_content; # or whatever
}
else {
die $response->status_line;
}
Hope that helps (TL;DR it looks like you needed a user agent set for this particular API call)
EDIT: the reason why it was working fine for the second URL you tried is because it's hitting a different API which doesn't seem to care about user-agent.
Upvotes: 7