akuk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am need to pass two parameters in the Dancer2 framework but unable to retrieve it from web page.

use Dancer2; use Dancer::Exception; use Plack::Builder; our $VERSION = '0.1'; # Enable JSON Serializer set serializer => 'JSON' get '/login:username:password' => sub { request->params; my %user = (); my $username = params->{username}; my $password = params->{password}; %user = ( user => { Name => $username, Pass => $password } ); return \%user; };

When I try to access it from the webpage http://localhost:5000?username=foo&password=bar I am getting 404 error. Did I messed up the perl code in the above app.pm package.

Replies are listed 'Best First'.
Re: Dancer2 Parameter Matching Failed
by Corion (Patriarch) on Jun 12, 2015 at 12:51 UTC

    Where did you find this syntax for the request and what does it do?

    get '/login:username:password' => sub {

    Maybe you wanted to just use

    get '/login' => sub {

      I have found this usage on http://search.cpan.org/~sukria/Dancer2-0.01/lib/Dancer2/Manual.pod

      Under " NAMED MATCHING A route pattern can contain one or more tokens (a word prefixed with ':'). Each token found in a route pattern is used as a named-pattern match. Any match will be set in the params hashref."

        I think the documentation is misleading here. Only elements of the path can be matched this way, with / as delimiter. So you will need:

        get '/login/:username/:password' => sub { ...

        But you should consider that such a GET request will need Javascript for logging in to the site, where a normal form submission will not need Javascript.

Re: Dancer2 Parameter Matching Failed
by Anonymous Monk on Jun 13, 2015 at 07:34 UTC
Re: Dancer2 Parameter Matching Failed
by Anonymous Monk on Jun 13, 2015 at 01:30 UTC

    When I try to access it from the webpage http://localhost:5000?username=foo&password=bar I am getting 404 error

    ? Hmm, how does that url relate to this one   get '/login:username:password' ?

    If you remove the host there is only ?user... ... which doesn't look like /login....

    See https://metacpan.org/pod/Dancer2::Manual#Declaring-Routes, URI

      Sorry, I forgot to mention /login in the above url. it is http://localhost:5000/login?username=foo&password=bar in this I got 404 error

        Sorry, I forgot to mention /login in the above url. it is http://localhost:5000/login?username=foo&password=bar in this I got 404 error

        Read the link, its the same problem, /login?... is not the same /login/foo/bar