Echo text with Bailador

After getting started with Bailador serving simple text, let's now see how can we accept input from the user and echo it back.

This isn't a complex task either, we are going slowly here.

Note! This site is about Perl 6.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.

Echo with POST

For this we had to create two routes as unfortunatelly at this point Bailador that came with Rakudo Star could not yet handle GET parameters.


use v6;
use Bailador;

get '/' => sub {
	'<form method="POST" action="/echo"><input name="text"><input type="submit"></form>';

post '/echo' => sub {
    my $text = request.params<text> // '';
	my $html = 'You said (in a POST request) ';
	$html ~= $text;
	return $html;


The positive side is that we can see how to create a route that will handle POST requests which was already working.

The first route get '/' => sub { will serve a GET request and it will return an HTML snippet which was included in the script. (I know, we'll start using templates soon.) That HTML snippet included a form with a single text-box and a submit button. The form has an action leading to the /echo URL, and it has a method="POST". This means that when the user clicks on the submit button, the browser will send back a POST request.

The second route we have post '/echo' => sub { will handle the POST request to the /echo path.

The request function provided by Bailador returns the object representing the current request in the form of a Bailador::Request object.

It has several methods, one of them is the params method that returns a hash where the keys are the parameter names (in our case it was text), and the values are, well, the values.

We save that value the $text variable, and we use //, the defined-or operator to set the variable to the empty string in case the user has not supplied any value. Then we concatenate that text using the ~ concatenation operator, to form an "html" string. Then we just send back that string and our small echo server is ready to work.

Echo with GET

Since the release of Rakudo Star 2014.12, Bailador was patched and it can now handle the GET requests as well. Here is how you could write the echo script:


use v6;
use lib '/home/gabor/work/Bailador/lib';
use Bailador;

get '/' => sub {
	'<form method="GET"  action="/echo"><input name="text"><input type="submit"></form>';

get '/echo' => sub {
    return 'You said (in a GET request) ' ~ (request.params<text> // '');


In this case I left out the temporary variables $text and $html, they were not necessary in the previous example either, but they could be used here too. In addition, I had to add the following to the script:

use lib '/home/gabor/work/Bailador/lib';

That points to the cloned GitHub repository of Bailador and it makes sure the script will load from this directory instead of the directory of Rakudo.

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-01-10


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.52904809

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.