[Catalyst] Catalyst::Controller::REST and Serializer/Deserializer

Francisco Obispo fobispo at isc.org
Mon Feb 25 07:44:21 GMT 2013


This is what I usually do:

1) install Catalyst::View::JSON

2) Add the view to your app: ./script/myapp_create.pl view JSON JSON

3) in the .conf file add:

<View::JSON>
  allow_callback 0
  expose_stash data
</View::JSON>

4) in the controller:

If the whole controller (.pm) will return JSON data, just add this to the end:


sub end : Private {
  my ( $self, $c ) = @_;
  if ( $c->stash->{data} ) {
    $c->forward('View::JSON');
    return 1;
  }

  return;
}


if you want to selectively send it in a method:


sub hello : Local {
  my ($self,$c) = @_;

  my $data={ hello => 'world!'};

  $c->stash->{data} = $data;
  $c->forward('View::JSON');

  return 1;

};


Hope this helps










On Feb 24, 2013, at 11:13 PM, Jean-Marc Choulet <jm130794 at gmail.com> wrote:

> Hello,
> 
> I'm new to Catalyst and I've certainly a stupid question... I use a Catalyst::Controller::REST to implement a web service. My question is : How can I force a application/json content-type response when I ask my ws with FF or Chrome... ?
> 
> Thanks
> 
> Jean-Marc
> 
> _______________________________________________
> 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/

Francisco Obispo 
Director of Applications and Services - ISC
email: fobispo at isc.org
Phone: +1 650 423 1374 || INOC-DBA *3557* NOC
PGP KeyID = B38DB1BE




More information about the Catalyst mailing list