http://www.perlmonks.org?node_id=1039713

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

    but it fails every time.

    How does it fail?

    How can I make the route call itself?

    I think you did

    $ lwp-request -UuSsEe "http://localhost/add_news?offset=2" GET http://localhost/add_news?offset=2 User-Agent: lwp-request/6.03 libwww-perl/6.05 302 Found Connection: keep-alive Date: Wed, 19 Jun 2013 04:13:40 GMT Location: http://localhost/add_news?offset=12 Server: Mojolicious (Perl) Content-Length: 0 Client-Date: Wed, 19 Jun 2013 04:13:40 GMT Client-Peer: 127.0.0.1:80 Client-Response-Num: 1 GET http://localhost/add_news?offset=12 User-Agent: lwp-request/6.03 libwww-perl/6.05 200 OK Connection: keep-alive Date: Wed, 19 Jun 2013 04:13:40 GMT Server: Mojolicious (Perl) Content-Length: 12 Content-Type: text/html;charset=UTF-8 Client-Date: Wed, 19 Jun 2013 04:13:40 GMT Client-Peer: 127.0.0.1:80 Client-Response-Num: 1 offset = 12 $ lwp-request -UuSsEe "http://localhost/add_news?offset=3000" GET http://localhost/add_news?offset=3000 User-Agent: lwp-request/6.03 libwww-perl/6.05 200 OK Connection: keep-alive Date: Wed, 19 Jun 2013 04:13:50 GMT Server: Mojolicious (Perl) Content-Length: 14 Content-Type: text/html;charset=UTF-8 Client-Date: Wed, 19 Jun 2013 04:13:50 GMT Client-Peer: 127.0.0.1:80 Client-Response-Num: 1 offset = 3000
    [Tue Jun 18 21:13:39 2013] [info] Listening at "http://localhost:80/". Server available at http://localhost:80/. [Tue Jun 18 21:13:40 2013] [debug] Your secret passphrase needs to be +changed!!! [Tue Jun 18 21:13:40 2013] [debug] GET "/add_news". [Tue Jun 18 21:13:40 2013] [debug] Routing to a callback. [Tue Jun 18 21:13:40 2013] [debug] 302 Found (0.001987s, 503.271/s). [Tue Jun 18 21:13:40 2013] [debug] GET "/add_news". [Tue Jun 18 21:13:40 2013] [debug] Routing to a callback. [Tue Jun 18 21:13:40 2013] [debug] 200 OK (0.000847s, 1180.638/s). [Tue Jun 18 21:13:50 2013] [debug] GET "/add_news". [Tue Jun 18 21:13:50 2013] [debug] Routing to a callback. [Tue Jun 18 21:13:50 2013] [debug] 200 OK (0.000711s, 1406.470/s).

      It fails by not redirecting back to add_news when I have an offset of less than 10. I am not home so I cannot run the exact code but when I get home I will answer with a better answer.

      I would expect to see something like the following.

      # calling /add_news?offset=2 offset=2 offset=10 #

      What I do see is the following

      # calling /add_news?offset=2 #
        Ugh! The offset=2 expectation is wrong above.
        # calling /add_news?offset=2 -- a redirection would have happened -- offset=10 #
      Wow. I must have been doing something wrong yesterday because I was getting different results yesterday.

      I figured out my problem. My nginx configuration is incorrect. I am happy that my Perl code is working.

      Thanks for the help.