简体   繁体   中英

How can I implement a RESTful API in Perl?

I'm trying to implement a RESTful API in Perl. My current idea is to simply parse the path_info with a regex then dispatch the request to the appropriate subroutine which will then spit out the JSON, XML or even XHTML for the requested resource.

For example to retrieve info about user 1234 the RESTful client should find it at:

http://example.com/model.pl/users/1234

Below is the skeleton code of my first attempt at implementing a RESTful API:

model.pl :

#!/usr/bin/perl -w
use strict;
use CGI;

my $q = CGI->new();

print $q->header('text/html');

my $restfuluri  = $q->path_info;

if      ($restfuluri =~ /^\/(questions)\/([1-9]+$)/) { questions($1, $2); }
elsif   ($restfuluri =~ /^\/(users)\/([1-9]+$)/)     { users($1, $2); }


sub questions
{
      my $object = shift;
      my $value  = shift;

      #This is a stub, spits out JSON or XML when implemented.
      print $q->p("GET question : $object -> $value");
}

sub users
{
      my $object = shift;
      my $value  = shift;

      #This is a stub, spits out JSON or XML when implemented.
      print $q->p("GET user: $object -> $value");
}

Before I proceed any further, I would like to hear from experienced Perl hackers whether I got the basic idea right and if there are any serious shortcomings with this approach in terms of performance.

I can imagine, after a while, the if/else block would grow really large.

Looking forward to hear your views to make this code better.

For lightweight REST APIs I would look at Mojolicious. The request routing is really straightforward and the inbuilt JSON renderer and user agent make development of simple REST APIs very straightforward in my experience.

If your app is going to be relatively small then Mojo::Lite may suit your requirements. For example you may be able to do something like this:

use Mojolicious::Lite;

get '/questions/(:question_id)' => sub {
    my $self = shift;
    my $result = {};
    # do stuff with $result based on $self->stash('question_id')
    return $self->render_json($result)
}

app->start;

I would use something like CGI::Application::Dispatch, it lets me build a dispatch table with variables and REST methods, and lets you use CGI and CGI::Application modules from CPAN. Eg:

table => [
'/questions/:id[get]'    => { rm => 'get_question' },
'/users/:id[get]'        => { rm => 'get_user' }, # OR
':app/:id[post]'         => { rm => 'update' }, # where :app is your cgi application module
':app/:id[delete]'       => { rm => 'delete' },
],

(or you can use auto_rest or auto_rest_lc)

you can use a separate CGI::Application class for each type of thing (or just use classes in your cgi-app controller class methods).

CGI::Application also comes with plugins for outputting XML, JSON or text generated from templates.

cgi-app (and c::a::d) are are CGI applications and can be used with (little or) no change under CGI, FastCGI or mod_perl. C::A::D is also a mod_perl PerlHandler by default too.

我会使用CatalystCatalyst::Controller::REST构建应用程序

Why not use the already implemented module Apache2::REST ? It is all there.

The simple solution:

 use CGI;

 my $page  = new CGI;

 if( $ENV{ 'REQUEST_METHOD' } eq 'GET' ){

    my $data = <<json;
    {
    "isbn" : "123456",
    "title" : "Programming Perl",
    "author" : "L. Wall"
     }
 json

     print $page->header('application/json');

     print $data;
 }

I know this has been a long time since this question has been asked, but I would like to provide some updated information.

There is a very useful module called Net::API::REST using Apache2 mod_perl behind.

You just need to set up Apache, and create your own module inheriting from Net::API::REST and create an init method in which you define your endpoints map, such as:

sub init
{
    my $self = shift( @_ );
    $self->{routes} =
    {
    # e.g. your API version 1
    1 =>
        {
        'favicon.ico' => $self->curry::noop,
        auth =>
            {
            google =>
                {
                _handler => $self->curry::oauth_google,
                callback => $self->curry::oauth_google(callback => 1),
                },
            linkedin =>
                {
                _handler => $self->curry::oauth_linkedin,
                callback => $self->curry::oauth_linkedin(callback => 1),
                },
            },
        },
        stripe => $self->curry::stripe,
    };
    $self->{api_version} = 1;
    $self->{supported_api_versions} = [qw( 1 )];
    $self->{default_methods} = [qw( GET POST )];
    $self->{supported_methods} = [qw( DELETE GET HEAD OPTIONS POST PUT )];
    $self->{supported_languages} = [qw( en-GB en fr-FR fr ja-JP )];
    $self->SUPER::init( @_ );
    return( $self );
}

And when someone access one of your endpoint, your corresponding method gets called in the context of a powerful environment to use to reply to the request. Check the module metacpan page for more information.

The Simple Solution - with a variable tweak on $data using qq~~; Keeps all code lined up.

 use CGI;

 my $page  = new CGI;

 if( $ENV{ 'REQUEST_METHOD' } eq 'GET' ){

    my $data = qq~
        {
            "isbn" : "123456",
            "title" : "Programming Perl",
            "author" : "L. Wall"
        }
    ~;

     print $page->header('application/json');

     print $data;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM