In this example we'll see how to accept POST parameters in a Dancer 1 application. We'll also see how to write tests for it.

Directory Layout

├── bin
│   └──
├── lib
│   └──
└── t
    └── 01-index.t

Code launching the app


#!/usr/bin/env perl
use Dancer;
use App;

The code of the application


package App;
use strict;
use warnings;
use Dancer ':syntax';

get '/' => sub {
    return q{
    <h1>Echo with POST</h1>
    <form action="/echo" method="POST">
    <input type="text" name="txt">
    <input type="submit" value="Send">

post '/echo' => sub {
    return 'You sent: ' . param('txt');


The test code


use Test::More tests => 2;
use strict;
use warnings;

# the order is important
use App;
use Dancer::Test;

subtest index => sub {
    my $resp = dancer_response GET => '/';
    is $resp->status, 200;
    like $resp->content, qr{<h1>Echo with POST</h1>};
    like $resp->content, qr{<form};

subtest echo_post => sub {
    my $resp = dancer_response POST => '/echo', {
        params => {
            txt => 'Hello World!',
    is $resp->status, 200;
    is $resp->content, 'You sent: Hello World!';

The call to dancer_response returns a Dancer::Respons object. Feel free to use its methods.

Running the tests

Be in the root directory of your project. (The common parent directory of bin, lib, and t.) and type:

prove -l


prove -lv

for more verbose output.