Subject: | Possible Bug on REST Plugin |
Date: | Mon, 3 Mar 2014 12:16:44 +0800 |
To: | bug-Dancer-Plugin-REST [...] rt.cpan.org |
From: | Pierre VIGIER <pierre.vigier [...] gmail.com> |
Hi,
I have a strange behaviour with the Dancer::Plugin::REST, and i don't know
if it is desired.
First of all, i'm using a MacBook Retina, with OS X maverick, Darwin Kernel
Version 13.1.0
pierre@mac:~$ perl -v
This is perl 5, version 12, subversion 4 (v5.12.4) built for
darwin-thread-multi-2level
pierre@mac:~$ perl -MDancer::Plugin::REST -e 'print
$Dancer::Plugin::REST::VERSION';
0.07
I create a tiny Dancer app to expose my problem:
#!/usr/bin/env perl
use Dancer;
use Dancer::Plugin::REST;
set serializer => 'JSON';
prepare_serializer_for_format;
resource 'tests' =>
get => sub { status_ok( { message => "get ok" } ); },
create => sub { status_created( { message => "create ok" } ); },
delete => sub { status_ok( { message => "ok" } ); },
update => sub { status_ok( { message => "ok" } ); };
dance;
Then i launch the app from command line:
pierre@mac:~/work/test$ ./app.pl
Show quoted text
>> Dancer 1.3121 server 10301 listening on http://0.0.0.0:3000
>> Dancer::Plugin::REST (0.07)
== Entering the development dance floor ...
Here are some curl result:
pierre@mac:~$ curl http://localhost:3000/tests/1
{
"message" : "get ok"
}
pierre@mac:~$ curl http://localhost:3000/tests/1.xml
<data message="get ok" />
pierre@mac:~$ curl http://localhost:3000/tests/1
<data message="get ok" />
pierre@mac:~$ curl http://localhost:3000/tests/1.yml
---
message: get ok
pierre@mac:~$ curl http://localhost:3000/tests/1
---
message: get ok
pierre@mac:~$
I set my default serializer to JSON, then, when i make a GET request
without format extension, i retrieve some json, however, if i make a GET
request with a format extension, the url without format extension use the
previous explicitly requested format.
If several user access my API, and they do not specify format, another user
can make their content-type to change, i can use a work around by creating
a hook after calling prepare_serializer_for_format, like that:
#!/usr/bin/env perl
use Dancer;
use Dancer::Plugin::REST;
set serializer => 'JSON';
prepare_serializer_for_format;
hook 'before' => sub {
my $format = params->{'format'};
return if defined $format;
set serializer => 'JSON';
};
resource 'tests' =>
get => sub { status_ok( { message => "get ok" } ); },
create => sub { status_created( { message => "create ok" } ); },
delete => sub { status_ok( { message => "ok" } ); },
update => sub { status_ok( { message => "ok" } ); };
dance;
Then, as i expect, i have:
pierre@mac:~$ curl http://localhost:3000/tests/1
{
"message" : "get ok"
}
pierre@mac:~$ curl http://localhost:3000/tests/1.xml
<data message="get ok" />
pierre@mac:~$ curl http://localhost:3000/tests/1
{
"message" : "get ok"
}
pierre@mac:~$
Is it a normal behaviour or do i forget something in the Plugin
initialization?
Thanks,
Pierre