Hello Fellow Monks,

I am trying to download the HTML of a URL which requires authentication (username and password). Using curl, I am able to successfully do this via:

curl https://example.com --cookie "session=536...035a" -o "HTML_output.txt"

where the session ID is the one I obtained via the browser's developer tools function.

I am trying to replicate this using a more "native" Perl approach and using the LWP module, I have the following code which allows me to get the HTML of a given URL that does not require any authentication:

use LWP; my $url = "https://example.com"; my $browser = LWP::UserAgent->new( ); my $resp = $browser->get($url); print $resp->content, "\n";

Is there a way using LWP (or some other module) to also include the session ID from the browser cookie?

Thank you in advance.

As a quick update, I have modified my code to use HTTP::Cookies::Netscape as follows:

use strict; use warnings; use LWP; use HTTP::Cookies::Netscape; my $url = "https://example.com"; my $cookie_jar = HTTP::Cookies::Netscape->new( file => "/path/to/cookies.txt", ); my $browser = LWP::UserAgent->new( ); $browser->cookie_jar( $cookie_jar ); my $resp = $browser->get($url); print $resp->content, "\n";

Unfortunately, when I run that, the response that comes back tells me that I still need to supply a proper username and password, even though my session is still valid on the browser. I used the Firefox add-on "cookies.txt" to download a cookies.txt file which looks like:

# Netscape HTTP Cookie File .example.com TRUE / TRUE 1984932267 session 5361...1 +329

In reply to Using LWP (or some other module) to Dowload HTML with Cookie Session ID by Leudwinus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.