sugarboy has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, Could any one give an example of how to access a webservice in perl using REST, i have done it with SOAP::Lite, but would like to know how to achieve the same using REST.
  • Comment on Using REST in perl to access webservices

Replies are listed 'Best First'.
Re: Using REST in perl to access webservices
by derby (Abbot) on May 26, 2007 at 11:30 UTC

    It really depends on the webservice. Some are pure REST but most are REST-RPC hybrids. Although the examples are mostly ruby and python, I would recommend RESTful Webservices.

    So what would a REST call look like in perl? Well, if you install lwp-request, from the command line, it may look like:

    $ GET -e http://foo.bar.com/something
    Now you would inspect the http status code for success or failure (that's why you need the -e) and then parse the response - whether that's XML or JSON or plaintext or whatever.

    If you're really lucky, there may all ready be a CPAN module for the service (Net::Amazon::S3). If you're going to write a REST client, LWP and XML::LibXML (or another XML parser) are going to be your friends. If you're going to write a pure rest service, CGI::Simple is the only module to support the http PUT method (I provided a patch to CGI but that hasn't made it's way to CPAN yet).

    -derby
Re: Using REST in perl to access webservices
by renodino (Curate) on May 26, 2007 at 23:40 UTC
    Keep in mind that REST's 2nd most important feature is its catchy acronym, (Its most important feature being its beautiful simplicity).

    You may be able to proceed with LWP::Simple calls (assuming stateless operations). LWP::Simple::Post may be needed *if* you know the right parameters, but I've had marginal luck getting the latter to function, so YMMV.

    If you need to PUT or DELETE, you'll have to dig into LWP::UserAgent and HTTP::Request.

    And once you've gotten your response to any of the above, you'll need a suitably capable XML processor...if you're lucky, just XML::Simple (if you're really lucky, just some crafty regexen).

    FWIW: DBD::Amazon is REST based and, by far, that was the easiest part of the package development. I'm always amazed at the BFD that is made over SOAP interfaces. I guess the Java/Web Services crowd can't abide a solution that's less then a KLOC.

    Go REST, young man, go REST!


    Perl Contrarian & SQL fanboy