Add a cookie and redirect
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.
On Thu, 4 Aug 2016 13:52:15 Amelia Ireland <aireland@lbl.gov> wrote:
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;
You're probably best just using the cookie keyword: cookie return_to => '/', expires => ..., domain => ... That said, using a cookie to set a redirect seems unnecessary. Can't you just put it in the redirect URL? Andy
That said, using a cookie to set a redirect seems unnecessary. Can't you just put it in the redirect URL?
No, unfortunately not -- I'm using an external authentication server and this is the method that it uses to return the user to wherever they were before logging in. :-\ Thanks for the tip -- the problem seems to be in testing the existence/value of the cookie. For unknown reasons, HTTP::Cookies doesn't seem to be parsing the cookie, even though it seems to be present in the HTTP::Headers object. On 4 August 2016 at 14:19, Andrew Beverley <andy@andybev.com> wrote:
On Thu, 4 Aug 2016 13:52:15 Amelia Ireland <aireland@lbl.gov> wrote:
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;
You're probably best just using the cookie keyword:
cookie return_to => '/', expires => ..., domain => ...
That said, using a cookie to set a redirect seems unnecessary. Can't you just put it in the redirect URL?
Andy
participants (2)
-
Amelia Ireland -
Andrew Beverley