Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Catalyst::Action::REST::ForBrowsers not detecting browser?

by uG (Scribe)
on Apr 17, 2011 at 19:10 UTC ( #899831=perlquestion: print w/ replies, xml ) Need Help??
uG has asked for the wisdom of the Perl Monks concerning the following question:

=head2 user =cut sub user :Local :ActionClass('REST::ForBrowsers') Args(1) { my($self, $c, $id) = @_; $c->stash(id => $id); } =head2 user_GET Handle GET requests. =cut sub user_GET_html : Private { my($self, $c) = @_; my $id = $c->stash->{id}; my $user = $c->model('ForumDB::User')->find({ id => $id }); if($user) { $self->edit_form->process( item => $user, params => $c->request->parameters, ); $self->status_ok($c, entity => { form => $self->edit_form->ren +der }); } else { $self->status_not_found($c, message => 'No matching user found +'); } } sub user_GET : Private { my($self, $c) = @_; my $id = $c->stash->{id}; my $user = $c->model('ForumDB::User')->find({ id => $id }); if($user) { $self->status_ok($c, entity => mk_user_entity($user)); } else { $self->status_not_found($c, message => 'No matching user found +'); } } sub mk_user_entity { my $user = shift; return { user => { id => $user->id, username => $user->username, password => $user->password, } }; }
According to http://search.cpan.org/~bobtfish/Catalyst-Action-REST-0.90/lib/Catalyst/Action/REST/ForBrowsers.pm
sub foo :Local :ActionClass('REST::ForBrowsers') { ... do setup for HTTP method specific handlers ... } sub foo_GET : Private { ... do something for GET requests ... } sub foo_GET_html : Private { ... do something for GET requests from browsers ... } sub foo_PUT : Private { ... do something for PUT requests ... }

Yet when I make an ajax call to my REST controller from my browser it still calls user_GET instead of user_GET_html Am I misunderstanding what this ActionClass does?


Here is the entire controller, just in case...

package Forum::Controller::REST; use Moose; use namespace::autoclean; __PACKAGE__->config(default => 'application/json'); use Forum::Form::User::Edit; BEGIN {extends 'Catalyst::Controller::REST'; } has 'edit_form' => ( isa => 'Forum::Form::User::Edit', is => 'rw', lazy => 1, default => sub { Forum::Form::User::Edit->new }, ); =head1 NAME Forum::Controller::REST - REST Controller =head1 DESCRIPTION REST Controller. =head1 METHODS =cut =head2 begin =cut sub auto : ActionClass('Deserialize') { my($self, $c) = @_; my $username = $c->req->header('X-Username'); my $password = $c->req->header('X-Password'); if( !$c->user && !$c->authenticate({username => $username, passwor +d => $password})) { $c->res->status(403); #forbidden $c->res->body("You are not authorized to use the REST API."); $c->detach; } } =head2 user =cut sub user :Local :ActionClass('REST::ForBrowsers') Args(1) { my($self, $c, $id) = @_; $c->stash(id => $id); } =head2 user_GET Handle GET requests. =cut sub user_GET_html : Private { my($self, $c) = @_; my $id = $c->stash->{id}; my $user = $c->model('ForumDB::User')->find({ id => $id }); if($user) { $self->edit_form->process( item => $user, params => $c->request->parameters, ); $self->status_ok($c, entity => { form => $self->edit_form->ren +der }); } else { $self->status_not_found($c, message => 'No matching user found +'); } } sub user_GET : Private { my($self, $c) = @_; my $id = $c->stash->{id}; my $user = $c->model('ForumDB::User')->find({ id => $id }); if($user) { $self->status_ok($c, entity => mk_user_entity($user)); } else { $self->status_not_found($c, message => 'No matching user found +'); } } sub mk_user_entity { my $user = shift; return { user => { id => $user->id, username => $user->username, password => $user->password, } }; }

Comment on Catalyst::Action::REST::ForBrowsers not detecting browser?
Select or Download Code
Replies are listed 'Best First'.
Re: Catalyst::Action::REST::ForBrowsers not detecting browser?
by Khen1950fx (Canon) on Apr 18, 2011 at 10:14 UTC
    If you think that it's not detecting the browser, then run this test. If everything's OK, then it should return "default is a browser".
    #!/usr/bin/perl use strict; use warnings; use Test::More; use Catalyst::Request; use Catalyst::TraitFor::Request::REST::ForBrowsers; use Catalyst::Request::REST::ForBrowsers; use Catalyst::Action::REST::ForBrowsers; use Moose::Meta::Class; use HTTP::Headers; my $anon_class = Moose::Meta::Class->create_anon_class( superclasses => ['Catalyst::Request'], roles => ['Catalyst::TraitFor::Request::REST::ForBrowsers'] +, cache => 1, )->name; for my $class ( $anon_class, 'Catalyst::Request::REST::ForBrowsers' ) +{ { for my $method (qw( GET )) { my $req = $class->new(); $req->method($method); $req->{_context} = 'MockContext'; $req->parameters( {} ); is( $req->method(), $method, ); } } { my $req = $class->new(); $req->{_context} = 'MockContext'; $req->method('GET'); $req->parameters( {} ); $req->headers( HTTP::Headers->new() ); ok( $req->looks_like_browser(), 'default is a browser' ); } package MockContext; sub prepare_body { } } done_testing;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://899831]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2015-07-29 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls