sylph001 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I'm not familiar with the many HTTP related modules.

I'm in urgent need to make a LWP GET request along with a predefined cookie in the header.

Is my following code correct and will it work?

I'm especially worried about the data structures I used.

345 use HTTP::Headers; 346 my $h = HTTP::Headers->new; 347 $h->header( Cookie => 'ASP.NET_SessionId=xaduwqpg5ocnenpb +0l0xsq4d; GMProfileInfo=e=xxxx@banana.com' ); 348 349 $dl_res = $ua->request( 350 GET $Url, 351 %$h, 352 content=> [ 353 %$asp_state 354 ] 355 );

 

 

Thanks much!

Replies are listed 'Best First'.
Re: Adding cookies into headers of LWP request
by McA (Priest) on Sep 25, 2014 at 14:52 UTC

    Hi,

    Looking at the documentation (quick look as you're in a hurry) the follwoing code should be enough:

    $dl_res = $ua->request( GET $Url, Cookie => 'ASP.NET_SessionId=xaduwqpg5ocnenpb0l0xsq4d; GMProfileInf +o=e=fis-mort-dev@banana.com', content=> [ %$asp_state ] );

    assuming that you just ask how to incorporate the Cookie header.

    Regards
    McA

      You're right. Although the code as given in the original example wont work. From my reading and a quick test, "Content" can only be used with the POST function and complains about not being passed a SCALAR with GET.

Re: Adding cookies into headers of LWP request
by sylph001 (Sexton) on Sep 25, 2014 at 15:40 UTC

    Thank you very much McA for the example.

    However when I tried this in my code, it seems the ASP sessionID is still missing in the cookie.

    What's more, the response was an html page instead of the real data that it should be.

    The web page it access seems needs two parts in the cookie, one is the ASP sessionID, the other is the mail address like thing as you could see from the cookie content.

    Is the cookie not correctly accept when I put it into the GET request?

    345 $dl_res = $ua->request( 346 GET $req_url, 347 Cookie => 'ASP.NET_SessionId=xaduwqpg5ocnenpb0l0xsq4d +; GMProfileInfo=e=xxxx@banana.com' 348 );
    The real request it sent by looking from the debugger (the ASP Session +ID is not showing up but it should): '_request' => bless( { '_content' => '', '_uri' => bless( do{\(my $o = 'http://www.xxxx.xxx/pages/profil +e.aspx/pages/profile.aspx?src=http://xxxx.xxxx.xxx/protectedfiledownl +oad.aspx?dlfile=data_bulk/file.txt')}, 'URI::http' ), '_headers' => bless( { 'cookie2' => '$Version="1"', 'user-agent' => 'libwww-perl/5.804', 'cookie' => 'GMProfileInfo=e=xxxx@banana.com&i=536' }, 'HTTP::Headers' ), '_method' => 'GET' }, 'HTTP::Request' )

      Things don't add up. Where for instance is that &i=536 in the cookie coming from?

      Since this all works here, i suspect that you might be hitting a bug in the version of libwww-perl you're using (5.804). For instance Bug 47667, has only been fixed since 5.815. Doesn't look like that actually applies in this case, but trying a newer version is worth a go.