Question

I want to write restful api via Catalyst, and use for this [Catalyst::Controller::REST][1].

I have written that code.

package addressbook::Controller::REST;
use strict;
use warnings;
use base qw(Catalyst::Controller::REST);


sub user :Local :ActionCLass('REST') :Args(1){
    my( $self, $c, $id ) = @_;

    $c->stash( id => $id );

}

# Get user
sub user_GET {
    my ( $self, $c ) = @_;

    my $user = $c->model('DB::User')->find( { id => $c->stash->{id} } );

    if ( $user ){
            $self->status_ok($c, entity => { firstname => $user->firstname } ); 
    }
    else {
            $self->status_not_found($c, message => 'No matching user'); 
    }
}

__PACKAGE__->config(default => 'text/x-json');
1;

Then i run the server, go to localhost:3000/rest/user/1 (i have user by that id) and get

Cannot find a Content-Type supported by your client.

I try to set PACKAGE->config application/json, text/xml, text/html, text/x-yaml ... but it's not help.

Any ideas?

Thanks.

Was it helpful?

Solution

As implemented, Catalyst Action REST does content negotiation on the request to determine the serialization method to be used. The default setting is only a fallback and normally your request in the real world will contain a content type.

Documentation on the supported content types and how to map in new deserializers can be found here: enter link description here. Note also that recent versions remove built in support for YAML which would have been the default response to text/html if you just requested the url in your browser.

Use curl or test from a real browser using Javascript

curl -H "Content-Type: application/json" http://localhost:3000/rest/user/1

Also check your installed version of Catalyst which will appear on the info line as you start up the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top