Home »
Perl
Performing POST request with Perl Dancer web framework
Here, we will quickly learn how we can make a post request in Perl Dancer web framework?
Submitted by Godwill Tetah, on December 11, 2020
This article requires basic knowledge of Perl dancer as perquisites.
POST is an HTTP method which simply means creating a resource at the server. We are going to use an example of creating a simple page that enables the user to type a little note he/she has and it will be displayed on the web page.
So, we will set up a route /note using get and post request. Then we will get content input in the HTML form and then display it then on the web page. The HTML form will be served as a view (temp.tt) or template.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {background-color: yellow;}
p {color: blueviolet;}
</style>
<title>DANCER POST</title>
</head>
<body>
<center>
<p>I love dancing with perl dancer</p>
<br>
<form method="POST">
<input type="text" name="note" placeholder="type your notes" /> <br> <br>
<input type="submit" value="POST">
</form>
</center>
</body>
</html>
index.pl
use Dancer;
set views => path( dirname(__FILE__), 'templates' );
get '/note' => sub {
template 'temp';
};
post '/note' => sub {
params->{note};
};
dance;
From the code at index.pl, the Post request is done using the post keyword followed by a subroutine. In the subroutine, notice how I got the notes written by the user in the html form. I simply used params which access the content of the name attribute of any form field.
Remember I gave the form field the name attribute as the value note.
Now run the code and see the output,