Hitting a WCF service from a Perl script on Linux, using JSON::RPC

Another post to show how different platforms can be connected together using services. I feel these kinds of examples are important to get buy in for Service Oriented Architectures within the enterprise.

I wrote up a little perl script running on linux that hits a WCF service installed on a windows box (running an IIS web server). In the script I use JSON::RPC, a freely available Perl module.

To run it you need to install 2 perl modules: JSON and JSON::RPC. I also use the Data::Dumper module to print out the results but you don’t have to. You can just write out the results on the screen using Perl’s print function.

Once you have the modules installed, here is the script that hits the service.

#!/usr/local/bin/perl

use JSON::RPC::Client;
use Data::Dumper;

my $client = new JSON::RPC::Client;

# URL for the WCF service
my $uri = 'http://mysrv.com/services/mysvc.svc/mysvcmethod;

# args to the service request, already jsonized
my $obj =  { "reportId" => 1, "projectId" => 126 };

# make the call
my $res = $client->call($uri, $obj);

if ($res) {
    if ($res->is_error) {
        print "Error : $res->error_message";
    }
    else {
        # everything looks good... print the response
        print Dumper($res);
    }
}
else {
     print $client->status_line;
}

Steps for installing required Perl modules

Here are some steps for installing the above mentioned Perl modules in your user directory, if you don’t have permissions to install them in your root directory.

First, download the tar files for the modules. Then:

cd ~
tar xzvf 
cd 
perl Makefile.PL PREFIX=/home/user/
make
make install
setenv PERL5LIB /home/user/lib/perl5/site_perl/5.8.5
Advertisements

About floatingfrisbee

A programmer/blogger from New York City
This entry was posted in web services and tagged , , . Bookmark the permalink.

One Response to Hitting a WCF service from a Perl script on Linux, using JSON::RPC

  1. Thanks for the script. I used your example to generate table from JSON file. Do you know if some how we can edit part of JSON file. here is the script I wrote:
    http://www.ssiddique.info/creating-html-table-using-json-file.html

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