Mojolicious lite how to redirect for not found and server error pages to user defined error page - perl

How to redirect user defined error page for not found and server error pages to user define page Mojolicious lite

You can add a template for your custom page named exception.html.ep or not_found.html.ep at the end of your liteapp.
For example:
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
$self->render(text => "Hello.");
};
app->start;
__DATA__
## not_found.html.ep
<!DOCTYPE html>
<html>
<head><title>Page not found</title></head>
<body>Page not found <%= $status %></body>
</html>
For a reference, see the Mojolicious rendering guide.
The renderer will always try to find exception.$mode.$format.* or
not_found.$mode.$format.* before falling back to the built-in default
templates.

I wanted to run some code in my 404 page so borrowing from here
https://groups.google.com/forum/#!topic/mojolicious/0wzBRnetiHo
I made a route that catches everything and placed it after all my other routes, so urls that don't match routes fall through to this:
any '/(*)' => sub {
my $self = shift;
$self->res->code(404);
$self->res->message('Not Found');
# 404
$self->stash( {
# ... my stuff in the stash ...
} );
$self->render('mytemplate', status => 404);
};

I had an API that wanted to send back 404 errors as it would any other error—same JSON format and whatnot.
I had this snippet at the end of startup (full app, not lite). Since this is the last defined route, it only picks up anything not already handled. And, since this handles all that, Mojo never gets the chance to use its own 404 handling by looking for templates:
$self->routes->any('/*')->to(
controller => 'Base',
action => 'not_found'
);

Related

Perl Mojolicious routes call sub only once

Iam using Mojolicious::Lite to declare routes for a webservice.
Maybe i missunderstood the routes behaviour but if iam calling a subroutine in the routes definition it gets only called once.
I thougt the sub should get triggered every time the webservice route is called... which isn't the case.
For example i wrote a test route:
use Mojolicious::Lite;
get '/test' => {
text => get_test()
};
sub get_test {
say 'Hello iam only showing up once';
return 'test';
};
This is the console output on starting the server and visiting the localhost:3000/test route:
Hello iam only showing up once
[2020-04-04 22:07:21.09011] [69050] [info] Listening at "http://*:3000"
Server available at http://127.0.0.1:3000
[2020-04-04 22:07:28.26033] [69050] [debug] [78278b87] GET "/test"
[2020-04-04 22:07:28.26097] [69050] [debug] [78278b87] 200 OK
(0.000626s, 1597.444/s)
The "Hello iam only showing up once" is outputted once as the server starts. Visiting the route is not triggering the sub again.
If this is the wanted behaviour how can i get my routes to triggering the sub everytime the route is visited?
I need this because i use this webservice in my application to scan the network and return the result. And i want to rescan the network everytime i call the webservice GET route. Otherwise the data would be useless if its not up to date.
Thanks for your help and explanations.
Your code isn't actually rendering. Your get sub needs to call the render method from Mojolicious::Controller. Inside the get sub, $_[0] is an instance of the controller. In a Mojolicious::Lite app, the route and controller are combined, so you do need to render. If you change your code to this, it will do what you might expect.
use Mojolicious::Lite;
get '/test' => sub {
shift()->render(text => get_test());
};
sub get_test {
warn "Hello I show up on every hit.\n";
return 'test';
};
The key difference is the shift()->render(...) call, which could also be written like this:
get '/test' => sub {
my $c = shift;
$c->render(text => get_text());
}
The above answer is good, but one of the key problems in your code is that you seem to have incorrectly defined your route code ref:
get '/test' => {
should be:
get '/test' => sub {
This is why your get_test only gets called once - you are defining a hash and populating it with the result of get_test, instead of defining a subroutine. But see #DavidO's answer for the full detail on how to approach this.

Problem with Mojo::IOLoop timer before redirect

i have a sub in my Mojolicious Controller which is called when a csv file is uploaded through a http post.
After the file is uploaded, a message gets rendered that say "you will be redirected in x seconds".
so i want to implement Mojo::IOLoop::Delay and as callback i use the redirect statement. But i get the following error by Morbo:
Mojo::Reactor::EV: Timer failed: Transaction already destroyed at /usr/local/share/perl/5.22.1/Mojolicious/Plugin/DefaultHelpers.pm line 168.
controller code:
sub upload {
my $self = shift;
# Check file size
return $self->render(text => 'File is too big.', status => 200)
if $self->req->is_limit_exceeded;
# Process uploaded file
return $self->redirect_to('/') unless my $newCsv = $self->param('fileToUpload');
my $size = $newCsv->size;
my $name = $newCsv->filename;
my $delay = 2;
$self->render(text => "Thanks for uploading $size byte file $name.<br>
You will be redirected in $delay seconds");
Mojo::IOLoop->timer($delay => sub {
$self->redirect_to('/');
});
}
relevant routes:
$r->get('/')->to(controller => 'main', action => 'index');
$r->post('/uploadCsv')->to(controller => 'main', action => 'upload')->name('uploadCsv');
Thank you in advance
redirect_to is effectively a render that renders a HTTP 302 response to redirect. You cannot render twice, so even if you keep the transaction around until the redirect_to call, it would have already rendered the page. So there are two actual options for what you are trying to do; render a page with javascript that will perform the redirect after a timeout, or render an HTML page with a meta refresh tag which will cause the page to redirect after a delay. This MDN page discusses both approaches.
upload returns after Mojo::IOLoop->timer and nothing waits for the timer. You can try using Mojo::IOLoop->delay and $delay->wait instead. But I am not sure how it works. So it might be equivalent to just sleep.
Do you really need to redirect from perl code? You can render some js with setTimeout for the same effect.
In fact I would recommend moving all text and redirecting to js and render only json with some status information inside upload. So you can implement better UI with error handling.

How to change Mojolicious Lite default error not found to a custom json response

I'm creating a json web service using Mojolicious Lite.
By default Mojolicious returns a HTML response for a server error or not found error.
Is there a way to overwrite this to a custom JSON response?
Here are two approaches:
Use json as the app's default format and use a not_found.*.json.ep template
use Mojolicious::Lite;
app->renderer->default_format('json');
app->start;
__DATA__
## not_found.development.json.ep
{"not":"found","code":404,"data":{"key1":"value1","key2":[42,19,"value3"]}}
Override json payload with a before_render hook.
use Mojolicious::Lite;
hook before_render => sub {
my ($c,$args) = #_;
if ($args->{template} && $args->{template} eq 'not_found') {
$args->{json} = { "too bad" => "so sad" };
}
};
app->start;
It's been a minute, but in Mojo 9 in a full app I've just been returning JSON and returning the status:
$c->render( json => $json, status => 404 );
But, I also have a catch-all route at the end of my setup:
$self->routes->any('/*')->to( ... );
Note, however, that there are some decisions to make about HTTP codes and application-level messaging. For example, accessing a defined and valid endpoint that returns zero search results could easily return 200 and an empty JSON array. The endpoint was there, the server knew how to handle it, and zero list items can be seen as valid as any other number. See I've been abusing HTTP Status Codes in my APIs for years, for example.
The Rendering guide discusses how to customize these responses.

Perl Mechanize follow_link is failing

I'm trying to login to a site using following code
my $mech = WWW::Mechanize->new(autosave=>1);
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->follow_link( text => 'Sign In');
$mech->click();
$mech->field(UserName => "$username");
$mech->field(Password => "$password");
$mech->submit();
But during follow_link the href contains two front slashes e.g (//test/sso-login) hence follow_link is considering it as whole URL and it's failing as below
Error GETing http://test/sso-login: Can't connect to test:80 (Bad hostname)
I can't change the href since it's our of my control. Is there a way to overcome this problem and make it take the full URL appending this href.
Sure. You can modify the HTML that Mech is looking at just before you call follow_link():
my $html = $mech->content;
$html =~ s[//test/sso-login][http://example.com/test/sso-login]isg;
$mech->update_html( $html );
See the documentation for details. Search for "update_html" on that page.

How do I use Perl's LWP to log in to a web application?

I would like to write a script to login to a web application and then move to other parts
of the application:
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use Data::Dumper;
$ua = LWP::UserAgent->new(keep_alive=>1);
my $req = POST "http://example.com:5002/index.php",
[ user_name => 'username',
user_password => "password",
module => 'Users',
action => 'Authenticate',
return_module => 'Users',
return_action => 'Login',
];
my $res = $ua->request($req);
print Dumper(\$res);
if ( $res->is_success ) {
print $res->as_string;
}
When I try this code I am not able to login to the application. The HTTP status code returned is 302 that is found, but with no data.
If I post username/password with all required things then it should return the home page of the application and keep the connection live to move other parts of the application.
You may be able to use WWW::Mechanize for this purpose:
Mech supports performing a sequence of page fetches including following links and submitting forms. Each fetched page is parsed and its links and forms are extracted. A link or a form can be selected, form fields can be filled and the next page can be fetched. Mech also stores a history of the URLs you've visited, which can be queried and revisited.
I'm guessing that LWP isn't following the redirect:
push #{ $ua->requests_redirectable }, 'POST';
Any reason why you're not using WWW::Mechanize?
I've used LWP to log in to plenty of web sites and do stuff with the content, so there should be no problem doing what you want. Your code looks good so far but two things I'd suggest:
As mentioned, you may need to make the requests redirectable
You may also need to enable cookies:
$ua->cookie_jar( {} );
Hope this helps