Access and download files with dynamic cookie using cURL

Should be able to read and write the same cookie, like

curl --cookie cookie_file --cookie-jar cookie_file https://...

or just

curl -b cookie_file -c cookie_file  https://...

In principle you'd use -c option to write a cookie file when/before logging in with your credentials and then use -b to read from it for the next request(s) but it should work in one as well.

See cookies in curl docs, and the Cookies chapter in Everything curl book.


Or, being in a Perl program we can use Perl's most extensive support for network programming.

One established set of tools for this rests around LWP::UserAgent, with a host of other classes. See an overview in LWP. The other prominent one has Mojo::UserAgent, in the middle of a whole web framework. I'll use LWP::UserAgent for an example below.

By default the cookie support is turned off so we first need to enable it, either via its attribute or using the method. Then the user-agent object will store all cookies (in an HTTP::Cookies object), managing and sending them as needed with each request.

use warnings;
use strict;
use feature 'say';

use LWP::UserAgent;

my $url = shift // die "Usage: $0 url\n";

my $ua = LWP::UserAgent->new;
$ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });
$ua->cookie_jar->save;

# How does the website expect a log in?

my $response = $ua->get( $url );

die $response->status_line if not $response->is_success;

say $ua->cookie_jar->as_string;
# ...

I can't include specific login code since it's not stated in the question how it need be done. If the site uses HTTP basic auth then credentials is a good way to do it, but if it is cookies based then you want to request the form for that etc (and it can use both).

Or one can set up cookies via attributes in the constructor instead

my $ua = LWP::UserAgent->new(
    cookie_jar => {
        file => "$ENV{HOME}/.cookies.txt",
        autosave => 1,
    }
);

If there is no need to save cookies in a file to enable cookies we can do simply

my $ua = LWP::UserAgent->new( cookie_jar => {} );

and don't need lines with ->cookie_jar (can still use it, like $ua->cookie_jar->as_string). Now a "cookie_jar" is kept in memory and discarded as the program ends.

Either way, running the program with the input string https://www14.sample.com (what that https://sample.com from the question resolves to for me) prints

Set-Cookie3: vsid=917vr3900381853629235; path="/"; domain=www14.sample.com; path_spec; expires="2027-01-17 07:56:25Z"; HttpOnly; version=0

(printed only for a demo)

Another way about this is to use WWW::Mechanize, in which cookies are taken care of, and more goodies provided. On the other end, there are Perl bindings for libcurl, module Net::Curl