1

I would like to use a suffix at the end of the URL in a Catalyst app to determine what format the response takes.

So http://foo/bar.json would result in a json response, http://foo/bar.xml in a xml one, and http://foo/bar in a plain HTML page.

I was trying to do that in an initial bloc - in a begin or auto action - by rewriting the URL that's fed to dispatching for subsequent actions, but that does not seem to work.

And suggestions about this? Including objections - it might not be such a good idea after all.

Thanks -

simone
  • 4,667
  • 4
  • 25
  • 47
  • 1
    How about making the urls of the form `http://foo.com/bar/json`, `http://foo.com/bar/xml`, etc., and then using this solution: http://stackoverflow.com/questions/4408408/what-is-the-best-way-to-hanlde-optional-url-arguments-in-a-catalyst-controller – i alarmed alien Sep 24 '14 at 08:40

1 Answers1

1
sub type : Regex('foo/bar(\.[^/]+)?') {
    my ( $self, $c ) = @_;
    my ( $type) = @{ $c->req->captures };
    $c->log->info("Type: ".$type);
    $c->response->body( $c->welcome_message );
}

You can try the above code, now based on the $type you will have to switch your view to json/xml/html.

sheeju
  • 56
  • 4