Trying to use Dancer2::Plugin::REST, what did I do wrong?
Hi, here is what I'm trying to do: package mbf; use Dancer2; use Dancer2::Plugin::REST; prepare_serializer_for_format; get '/page.:format' => sub { [ 'some key' => 'value', 'something'=> 'else', ] }; true; And when I go to http://localhost:5000/page.json<http://localhost:5000/markets.json>I get ARRAY(0x23b3880) in the browser. The data was not serialized. What did I do wrong? As an alternative I've done something that works: package mbf; use Dancer2; use Dancer2::Serializer::JSON; get '/page.json' => sub { to_json([ 'some key' => 'value', 'something'=> 'else', ]); }; true; And in the browser I get what I expect: ["some key","value","something","else"] I would like to use use Dancer2::Plugin::REST though, to leave the choice of format to whoever will use the API. I noticed a detail in the module's documentation. The two examples given are: get '/user/:id.:format' => sub { and get qr{^/user/(?<id>\d+)\.(?<format>\w+)} => sub { The first example is similar to my case, but not the second one. And the documentation says: "Regexp routes will use the file-extension from captures->{'format'} to determine the serialization format." Which seems to indicate that only the second use case in the documentation would work. I tried it. It didn't work for me either. I must be doing something wrong. Any idea? --- Pierre Masci
I'd like to say this is a bug in Dancer2::Plugin::REST but it has passing tests, so I'm not exactly sure where the problem is. I played with it for a bit and couldn't track it yet. I hope to spend more time understanding this, but unfortunately I probably won't be able to. May I suggest taking a look at the tests and working your way back to your code? On Thu, Dec 5, 2013 at 2:23 PM, Pierre M <piemas25@gmail.com> wrote:
Hi, here is what I'm trying to do:
package mbf; use Dancer2; use Dancer2::Plugin::REST; prepare_serializer_for_format;
get '/page.:format' => sub { [ 'some key' => 'value', 'something'=> 'else', ] }; true;
And when I go to http://localhost:5000/page.json<http://localhost:5000/markets.json>I get ARRAY(0x23b3880) in the browser. The data was not serialized. What did I do wrong?
As an alternative I've done something that works:
package mbf; use Dancer2; use Dancer2::Serializer::JSON;
get '/page.json' => sub { to_json([ 'some key' => 'value', 'something'=> 'else', ]); }; true;
And in the browser I get what I expect: ["some key","value","something","else"] I would like to use use Dancer2::Plugin::REST though, to leave the choice of format to whoever will use the API.
I noticed a detail in the module's documentation. The two examples given are: get '/user/:id.:format' => sub { and get qr{^/user/(?<id>\d+)\.(?<format>\w+)} => sub {
The first example is similar to my case, but not the second one. And the documentation says: "Regexp routes will use the file-extension from captures->{'format'} to determine the serialization format." Which seems to indicate that only the second use case in the documentation would work. I tried it. It didn't work for me either. I must be doing something wrong. Any idea?
--- Pierre Masci
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On 13-12-06 07:51 AM, sawyer x wrote:
I'd like to say this is a bug in Dancer2::Plugin::REST but it has passing tests, so I'm not exactly sure where the problem is. I played with it for a bit and couldn't track it yet.
I can reproduce the issue with Pierre's code. Weird. I'll have a deeper look. Joy, `/anick
On 13-12-06 11:22 AM, Yanick Champoux wrote:
I can reproduce the issue with Pierre's code. Weird. I'll have a deeper look.
Urgh. So it seems that the response object is created before the serializer is set. And, even worse, it's not the response's own serializer that is set, but the global serializer for the application. Which means that we have this delightful delayed slapstick act: $ curl localhost:3000/page.json HASH(0x32fafd8) $ curl localhost:3000/page.yaml {"status":"404","title":"Error 404 - Not Found","message":"unsupported format requested: yaml"} $ curl localhost:3000/page.yml {"some key":"value","something":"else"} $ curl localhost:3000/page.json --- some key: value something: else Github issue will be created. Problem shall be solved. But only after my coffee's drained. :-) Joy, `/anick
You are fantastic. On Fri, Dec 6, 2013 at 6:30 PM, Yanick Champoux <yanick@babyl.dyndns.org>wrote:
On 13-12-06 11:22 AM, Yanick Champoux wrote:
I can reproduce the issue with Pierre's code. Weird. I'll have a deeper look.
Urgh.
So it seems that the response object is created before the serializer is set. And, even worse, it's not the response's own serializer that is set, but the global serializer for the application. Which means that we have this delightful delayed slapstick act:
$ curl localhost:3000/page.json HASH(0x32fafd8) $ curl localhost:3000/page.yaml {"status":"404","title":"Error 404 - Not Found","message":"unsupported format requested: yaml"} $ curl localhost:3000/page.yml {"some key":"value","something":"else"} $ curl localhost:3000/page.json --- some key: value something: else
Github issue will be created. Problem shall be solved. But only after my coffee's drained. :-)
Joy, `/anick _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
Indeed :) After reading your message I'm glad I didn't have to investigate myself: I would have struggled On 7 Dec 2013 10:50, "sawyer x" <xsawyerx@gmail.com> wrote:
You are fantastic.
On Fri, Dec 6, 2013 at 6:30 PM, Yanick Champoux <yanick@babyl.dyndns.org>wrote:
On 13-12-06 11:22 AM, Yanick Champoux wrote:
I can reproduce the issue with Pierre's code. Weird. I'll have a deeper look.
Urgh.
So it seems that the response object is created before the serializer is set. And, even worse, it's not the response's own serializer that is set, but the global serializer for the application. Which means that we have this delightful delayed slapstick act:
$ curl localhost:3000/page.json HASH(0x32fafd8) $ curl localhost:3000/page.yaml {"status":"404","title":"Error 404 - Not Found","message":"unsupported format requested: yaml"} $ curl localhost:3000/page.yml {"some key":"value","something":"else"} $ curl localhost:3000/page.json --- some key: value something: else
Github issue will be created. Problem shall be solved. But only after my coffee's drained. :-)
Joy, `/anick _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On 13-12-07 05:49 AM, sawyer x wrote:
You are fantastic.
I would have gone with "stubborn, and hunted down bugs in that piece of the code before", but I'll take that. ^.^ A new version of Dancer2::Plugin::REST is on its way to CPAN, with new tests and all that jazz. Please give it a whirl, and if anything look funny, gimme a poke. (Seasonal) joy, `/anick
I'm leaving for a two weeks holidays this afternoon. I will investigate that when I'm back. --- Pierre Masci I check email a couple times daily; to reach me sooner, you can send me a text message via this page: https://awayfind.com/mascip On 5 December 2013 13:23, Pierre M <piemas25@gmail.com> wrote:
Hi, here is what I'm trying to do:
package mbf; use Dancer2; use Dancer2::Plugin::REST; prepare_serializer_for_format;
get '/page.:format' => sub { [ 'some key' => 'value', 'something'=> 'else', ] }; true;
And when I go to http://localhost:5000/page.json<http://localhost:5000/markets.json>I get ARRAY(0x23b3880) in the browser. The data was not serialized. What did I do wrong?
As an alternative I've done something that works:
package mbf; use Dancer2; use Dancer2::Serializer::JSON;
get '/page.json' => sub { to_json([ 'some key' => 'value', 'something'=> 'else', ]); }; true;
And in the browser I get what I expect: ["some key","value","something","else"] I would like to use use Dancer2::Plugin::REST though, to leave the choice of format to whoever will use the API.
I noticed a detail in the module's documentation. The two examples given are: get '/user/:id.:format' => sub { and get qr{^/user/(?<id>\d+)\.(?<format>\w+)} => sub {
The first example is similar to my case, but not the second one. And the documentation says: "Regexp routes will use the file-extension from captures->{'format'} to determine the serialization format." Which seems to indicate that only the second use case in the documentation would work. I tried it. It didn't work for me either. I must be doing something wrong. Any idea?
--- Pierre Masci
participants (3)
-
Pierre M -
sawyer x -
Yanick Champoux