Search code examples
perlcookieslwp

Perl LWP: Send Cookie by HTTP header not work


I have known that if you want to use cookie, you must write code as:

$ua = LWP::UserAgent->new();
$ua->cookie_jar(HTTP::Cookies->new());

then you can get(), post() and so on.

But if I just put the cookie string into the HTTP HEADER and get(), I can not get the correct response. As:

$ua = LWP::UserAgent->new();
$ua->get($url, 'Cookie' => $cookie_string);

Solution

  • I think you will find that the Cookie header is being set correctly using that method.

    To make sure, you can write

    my $resp = $ua->get('http://www.myurl.com', Cookie => 'my=data');
    
    print $resp->request->as_string;
    

    and you should see that the request contains the line

    Cookie: my=data