Home > Uncategorized > Using basic #HTTP authentication with a #REST #API in Perl

Using basic #HTTP authentication with a #REST #API in Perl

perl-camel-source-code-1024-postbit-98365.jpg

Another language in the series, is Perl, where I’ve written some code to call the CarRegistrationAPI.com API – which is a JSON based REST API. Using Perl.

So, here is the perl module

package InfiniteLoop::CarRegistration;

use LWP::UserAgent;
use MIME::Base64;
use JSON;

use Exporter qw(import);

our @EXPORT_OK = qw(usa_lookup australia_lookup europe_lookup);

sub usa_lookup
{
my (%params) = @_;
my $server_endpoint = “https://www.regcheck.org.uk/api/json.aspx/CheckUSA/” . %params{‘registration_number’} . “/” . %params{‘state’};
%arguments = ( ‘url’ => $server_endpoint,
‘username’ => “” . %params{‘username’},
‘password’ => “” . %params{‘password’});
lookup(%arguments);
}

sub australia_lookup
{
my (%params) = @_;
my $server_endpoint = “https://www.regcheck.org.uk/api/json.aspx/CheckAustralia/” . %params{‘registration_number’} . “/” . %params{‘state’};
%arguments = ( ‘url’ => $server_endpoint,
‘username’ => “” . %params{‘username’},
‘password’ => “” . %params{‘password’});
lookup(%arguments);
}

sub europe_lookup
{
my (%params) = @_;
my $server_endpoint = “https://www.regcheck.org.uk/api/json.aspx/” . %params{‘endpoint’} . “/” . %params{‘registration_number’};
%arguments = ( ‘url’ => $server_endpoint,
‘username’ => “” . %params{‘username’},
‘password’ => “” . %params{‘password’});
lookup(%arguments);
}

sub lookup
{
my (%params) = @_;
my $ua = LWP::UserAgent->new;
my $encoded = encode_base64(%params{‘username’} . ‘:’ . %params{‘password’});
my $req = HTTP::Request->new(GET => “” . %params{‘url’});
$req->header(‘Authorization’ => ‘Basic ‘ . $encoded);
my $resp = $ua->request($req);
if ($resp->is_success) {
my $message = $resp->decoded_content;
my $json = JSON->new;
my $data = $json->decode($message);
return $data;
}
else {
print “HTTP GET error code: “, $resp->code, “\n”;
print “HTTP GET error message: “, $resp->message, “\n”;
}
}

Copy that perl module to perl/vendor

And here is a perl script to call it:

 

use InfiniteLoop::CarRegistration qw(usa_lookup australia_lookup europe_lookup);

### code examples

%params = ( ‘endpoint’ => ‘Check’,
‘registration_number’ => ‘Po14ryh’,
‘username’ => ‘** your username **’,
‘password’ => ‘** your password **’);

$data = europe_lookup(%params);
print $data->{Description} . “\n”;

%params2 = ( ‘registration_number’ => ‘hij934’,
‘state’ => ‘pr’,
‘username’ => ‘** your username **’,
‘password’ => ‘** your password **’);

$data2 = usa_lookup(%params2);
print $data2->{Description} . “\n”;

%params3 = ( ‘registration_number’ => ‘490JLN’,
‘state’ => ‘QLD’,
‘username’ => ‘** your username **’,
‘password’ => ‘** your password **’);

$data3 = australia_lookup(%params3);
print $data3->{Description}. “\n”;

You’ll obviously need your own username and password to call this 🙂

This code will hopefully shortly be up on Github and CPan.
 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: