[dancer-users] Add a cookie and redirect

Amelia Ireland aireland at lbl.gov
Thu Aug 4 21:52:15 BST 2016


Hi all,

I would like to add an extra cookie to a request and redirect it. The code
that I would think should work does not appear to be doing so, though.

Here's the route in question:

any '/login' => sub {

my $c = Dancer2::Core::Cookie->new(
name => 'return_to',
value => '/',
domain => config->{sso_domain},
expires => '5 mins'
);

push_response_header 'Set-Cookie' => $c->to_header;

redirect 'http://example.com';
};


When I test this code, though, nothing shows up in the cookie jar:

my $res = $app->request( GET '/login' );

is( $res->code, 302, '[GET /login] Correct code' );

is(
$res->headers->header('Location'),
'http://example.com',
'Correct Location header',
);

$jar->extract_cookies( $res );
say 'cookies: ' . $jar->as_string;
# output: "cookies: " -- i.e. no cookies found

say 'headers: ' . Dumper $res->headers;
# output:
headers: bless( {
  "::std_case" => {
    cookie => "Cookie",
    "set-cookie" => "Set-Cookie",
    "set-cookie2" => "Set-Cookie2"
  },
  "content-length" => 303,
  "content-type" => "text/html; charset=utf-8",
  cookie => "return_to=%2F; path=/; expires=Thu, 04-Aug-2016 20:47:05 GMT;
domain=example.com",
  location => "http://example.com"
}, 'HTTP::Headers' )

I'm not sure where the problem is -- am I setting the cookie incorrectly?
Does it not get added to the redirected request?

Any suggestions gratefully received! Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20160804/a06ead1a/attachment.html>


More information about the dancer-users mailing list