onelander has asked for the wisdom of the Perl Monks concerning the following question:
I am working on a script which the user calls a route called /add_news but that route should be able to call itself with a query parameter. This is the smallest amount of code I can come up with to do what I want but it fails every time. The Mojolicious documentation is not clear enough for me to understand what I need to do.
#!/usr/bin/perl use Mojolicious::Lite; get '/add_news' => sub { my $self = shift; my $offset = $self->param('offset') // 0; if ( $offset > 9 ) { $self->render(text => "offset = $offset\n"); } else { $self->redirect_to('/add_news?offset=10'); } }; app->start;
When the code runs with and the offset is greater than 10 I see the rendered text but once I use an offset less than 10 I get a 302 error. I understand it is redirecting but when I look at the output the URL looks correct.
[Tue Jun 18 23:24:49 2013] [info] Listening at "http://*:3000". Server available at http://127.0.0.1:3000. [Tue Jun 18 23:24:57 2013] [debug] Your secret passphrase needs to be +changed!!! [Tue Jun 18 23:24:57 2013] [debug] GET /add_news (Mozilla/5.0 (X11; Li +nux x86_64; rv:24.0) Gecko/20130618 Firefox/24.0). [Tue Jun 18 23:24:57 2013] [debug] Routing to a callback. [Tue Jun 18 23:24:57 2013] [debug] 200 OK (0.000952s, 1050.420/s). [Tue Jun 18 23:25:00 2013] [debug] GET /add_news (Mozilla/5.0 (X11; Li +nux x86_64; rv:24.0) Gecko/20130618 Firefox/24.0). [Tue Jun 18 23:25:00 2013] [debug] Routing to a callback. [Tue Jun 18 23:25:00 2013] [debug] 302 Found (0.000915s, 1092.896/s). [Tue Jun 18 23:25:00 2013] [debug] GET /add_news (Mozilla/5.0 (X11; Li +nux x86_64; rv:24.0) Gecko/20130618 Firefox/24.0). [Tue Jun 18 23:25:00 2013] [debug] Routing to a callback. [Tue Jun 18 23:25:00 2013] [debug] 200 OK (0.000518s, 1930.502/s).
I am using the following curl commands.
curl http://127.0.0.1:3000/add_news?offset=10 and curl http://127.0.0.1:3000/add_news?offset=2
How can I make the route call itself?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Mojolicious and calling current route
by Anonymous Monk on Jun 19, 2013 at 04:15 UTC | |
by onelander (Sexton) on Jun 19, 2013 at 11:56 UTC | |
by onelander (Sexton) on Jun 19, 2013 at 12:00 UTC | |
by onelander (Sexton) on Jun 19, 2013 at 23:19 UTC | |
by onelander (Sexton) on Jun 20, 2013 at 00:11 UTC |
Back to
Seekers of Perl Wisdom