The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Dancer::Response - Response object for Dancer

SYNOPSIS

# create a new response object
Dancer::Response->new(
    status => 200,
    content => 'this is my content'
);

Dancer::Response->status; # 200

# fetch current response object
my $response = Dancer::Response->current;

# fetch the current status
$response->status; # 200

# change the status
$response->status(500);

PUBLIC API

new

Dancer::Response->new(
    status  => 200,
    content => 'my content',
    headers => HTTP::Headers->new(...),
);

create and return a new Dancer::Response object

current

my $response = Dancer::Response->current();

return the current Dancer::Response object, and reset the object

exists

if ($response->exists) {
    ...
}

test if the Dancer::Response object exists

set

Dancer::Response->set(Dancer::Response->new(status=>500));

Set a new Dancer::Response object as the current response

content

# get the content
my $content = $response->content;
my $content = Dancer::Response->content;

# set the content
$response->content('my new content');
Dancer::Response->content('my new content');

set or get the content of the current response object

status

# get the status
my $status = $response->status;
my $status = Dancer::Response->status;

# set the status
$response->status(201);
Dancer::Response->status(201);

set or get the status of the current response object

content_type

# get the status
my $ct = $response->content_type;
my $ct = Dancer::Response->content_type;

# set the status
$response->content_type('application/json');
Dancer::Response->content_type('application/json');

set or get the status of the current response object

pass

$response->pass;
Dancer::Response->pass;

set the pass value to one for this response

has_passed

if ($response->has_passed) {
    ...
}

if (Dancer::Response->has_passed) {
    ...
}

test if the pass value is set to true

halt

Dancer::Response->halt();
$response->halt;

halted

if (Dancer::Response->halted) {
   ...
}

if ($response->halted) {
    ...
}
# set the header
$response->header('X-Foo' => 'bar');
Dancer::Response->header('X-Foo' => 'bar');

# get the header
my $header = $response->header('X-Foo');
my $header = Dancer::Response->header('X-Foo');

get or set the value of a header

headers

$response->headers(HTTP::Headers->new(...));
Dancer::Response->headers(HTTP::Headers->new(...));

return the list of headers for the current response

headers_to_array

my $headers_psgi = $response->headers_to_array();
my $headers_psgi = Dancer::Response->headers_to_array();

this method is called before returning a PSGI response. It transforms the list of headers to an array reference.