Setting the HTTP status in Dancer's before hook with the status keyword doesn't work with the exception of 301 and 302 (redirects).

To solve this problem, you can define first a special route for setting the HTTP status:

get '/bad_request' => sub {

status 400;

return 'Bad request.';


And advise Dancer to run this route from the before hook

on a certain condition:

hook before => sub {

if (! param('customer_id')) {