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

I wish to retrieve the contents of a webpage which requires a cookie to be set in the header of its request. Unfortunately, I can't seem to find a simple way to add the cookie to the header of the request. The Http::Cookies add_cookie_header method seems to require that the cookie/value pair already be stored in a file of unknown format. Any help on how I can add a simple cookie/value pair to the header of a request without file involement would be greatly appreciated. (something like &add_cookie($request, "username" => "Joe").

Replies are listed 'Best First'.
Re: client http request (take 2)
by chipmunk (Parson) on Jan 05, 2001 at 20:17 UTC
    You seem to have misinterpreted the documentation for HTTP::Cookies. The add_cookie_header() method simply requires that your HTTP::Cookies object (called $cookie_jar in the examples) contains a cookie. The cookie is created using the set_cookie() method. There's also an extract_cookie() method for getting a cookie from an HTTP::Response object.

    The save() and load() methods, for saving cookies to a file and loading them from a file, can be ignored if you don't want to save cookies to a file.

Re: client http request (take 2)
by extremely (Priest) on Jan 05, 2001 at 21:03 UTC
    If you have the LWP package installed (and it would seem that you do, you can manually do what you want with the included 'lwp-request' script. It has a '-H' switch that lets you set arbitrary headers.

    In a script, you can do something like:

    #!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTTP::Request; my $ua = LWP::UserAgent->new; my $rq= HTTP::Request->new(GET => 'http://place.to.go.web/'); $rq->header("Cookie", "username=Joe"); # see this? my $res=$ua->request($rq); print $res->as_string

    --
    $you = new YOU;
    honk() if $you->love(perl)