[Catalyst] RESTful example apps?

Benjamin Hitz hitz at stanford.edu
Thu Sep 15 16:38:02 GMT 2011


It seems odd to manually set the response to '200' like that.  Is this just a feature of "true" REST protocol, or you are not assuming anything about the server...

Ben
On Sep 15, 2011, at 5:53 AM, <Ian.Docherty at nomura.com> <Ian.Docherty at nomura.com> wrote:

> -----Original Message-----
> From: Gavin Henry [mailto:gavin.henry at gmail.com] 
> Sent: 15 September 2011 13:21
> To: The elegant MVC web framework
> Subject: [Catalyst] RESTful example apps?
> 
> Hi,
> 
> I was wondering if any one has any example code implement a REST API
> in Catalyst?
> 
> Thanks.
> 
> [Ian replied.] 
> 
> This is roughly what I tend to do...
> 
> package MyApp::Web::Controller::Rest;
> use Moose;
> use Readonly;
> 
> BEGIN {
>    extends 'Catalyst::Controller::REST';
> }
> my $base_url = '/rest';
> 
> Readonly::Scalar our $HTTP__OK                              => 200;
> Readonly::Scalar our $HTTP__BAD_REQUEST                     => 400;
> Readonly::Scalar our $HTTP__FORBIDDEN                       => 403;
> Readonly::Scalar our $HTTP__NOT_FOUND                       => 404;
> 
> sub foo : Local : ActionClass('REST::ForBrowsers') {}
> 
> *status_GET_foo = \&foo_GET;
> sub foo_GET : Private {
>    my ($self, $c) = @_;
> 
>    # Response
>    my $response = {
>        message     => 'hello world',
>        bar         => 123,
>    };
> 
>    $c->stash->{rest} = $response;
>    $c->response->status($HTTP__OK);
> }
> 
> sub bam : Local : ActionClass('REST::ForBrowsers') {}
> 
> *status_GET_bam = \&bam_GET;
> sub bam_GET : Private {
>    my ($self, $c) = @_;
> 
>    # response
>    my $response = {
>        message     => 'this is bam',
>        bar         => 456,
>    };
> 
>    $c->stash->{rest} = $response;
>    $c->response->status($HTTP__OK);
> }
> 
> sub bam_POST : Private {
>    my ($self, $c) = @_;
> 
>    # POST your input data here
> 
>    # Response
>    my $response = {
>        message => 'success',
>        status  => 1,
>    };
>    $c->stash->{rest} = $response;
>    $c->response->status($HTTP__OK);
> }
> 1;
> 
> -- 
> 
> 
> 
> 
> This e-mail (including any attachments) is confidential, may contain
> proprietary or privileged information and is intended for the named
> recipient(s) only. Unintended recipients are prohibited from taking action
> on the basis of information in this e-mail and must delete all copies.
> Nomura will not accept responsibility or liability for the accuracy or
> completeness of, or the presence of any virus or disabling code in, this
> e-mail. If verification is sought please request a hard copy. Any reference
> to the terms of executed transactions should be treated as preliminary only
> and subject to formal written confirmation by Nomura. Nomura reserves the
> right to monitor e-mail communications through its networks (in accordance
> with applicable laws). No confidentiality or privilege is waived or lost by
> Nomura by any mistransmission of this e-mail. Any reference to "Nomura" is
> a reference to any entity in the Nomura Holdings, Inc. group. Please read
> our Electronic Communications Legal Notice which forms part of this e-mail:
> http://www.Nomura.com/email_disclaimer.htm
> 
> 
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/

--
Ben Hitz 
Senior Scientific Programmer ** Saccharomyces Genome Database ** GO Consortium
Stanford University ** hitz at stanford.edu







More information about the Catalyst mailing list