Mojo::UserAgent on start method interferring with cookies?

125 views
Skip to first unread message

Adam McLean

unread,
Jan 29, 2014, 11:32:19 AM1/29/14
to mojol...@googlegroups.com
I'm using:

my $ua=Mojo::UserAgent->new();
$ua->on(start => sub {
       my ($ua, $tx)=@_;
       $tx->req->headers->header("accept" => "application/json");
       $tx->req->url->scheme("http");
       $tx->req->url->host("localhost");
       $tx->req->url->port("3000");
});

In my script to make my life easier interacting with a RESTful service.

I then call:
$ua->post("/authenticate" => json => { "user" => $user, "password" => $password });
Which responds with a 'set-cookie' header.

I can verify that $ua->cookie_jar contains the cookie that was sent.

I make a subsequent call to:
$ua->post("/edit")

However the cookie data is not sent in the request header.

If I strip out the 'on start' code, the cookie is handled as I would expect (included in the request header to edit).

Did I miss something?  Is this a poor use of the 'on start' technique and should be avoided?

Keita Sugama

unread,
Jan 30, 2014, 12:26:42 AM1/30/14
to mojol...@googlegroups.com
Hi Adam.

I use start event for some reason and cookies seem to be sent properly.
Doesn't your problem have something to do with redirection?

I just found that Mojo::UserAgent doesn't set User-Agent header for redirected requests.
I don't know this is bug or not but the request may causes the server side app
to close the session for security reason.

I'm testing with CakePHP for server side.

When $ua->max_redirects(5),

POST /login (with User-Agent header)
-> redirected to /index with Set-Cookie for PHPSESSID

GET /index (with Cookie header but no User-Agent header)
-> session deleted by CakePHP

To prevent this, I set the user_agent manually for now.


$ua->on(start => sub {
    my ($ua, $tx) = @_;
    $tx->req->headers->user_agent('My Agent');
});



2014-01-30 Adam McLean <kapur...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages