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

I'm writing a cgi program that gets a web page that contains a form. I want to automatically fill this form out and have the next page send to my browser from the cgi script. But the web page that is returned wants to set a cookie and redirect before I can continue on to the next page. Is there anyway to pass the cookie to my cgi script, then get my cgi script to set the cookie on the client pc?Thanks
Chris

Replies are listed 'Best First'.
Re: Pass cookies from one CGI to another
by Ido (Hermit) on Jan 27, 2002 at 22:34 UTC
    If you use LWP::UserAgent you can use HTTP::Cookies to create a cookie jar, like:
    use HTTP::Cookies; $cookie_jar = HTTP::Cookies->new;
    Then set this cookie_jar to be used in the user agent.
    $ua->cookie_jar([$cookie_jar])
    You can extract and add cookie like:
    $cookie_jar->extract_cookies($response); $cookie_jar->set_cookie($version, $key, $val, $path, $domain, $port, $ +path_spec, $secure, $maxage, $discard, \%rest)
    Hope that helps..
Re: Pass cookies from one CGI to another
by atcroft (Abbot) on Jan 27, 2002 at 22:49 UTC

    So you're loading perl CGI A which is pulling in page B containing a form, filling in the values, and submitting it, getting returned page C, which wants to redirect you to page D, and page D wants to set a cookie? And you want the script to hand back page D, correct?

    One problem I can see potentially is that when you return page D to the browser, it may try to set cookies again. I might be wrong in this, though.

    I haven't played much with writing web-clients in perl (although an upcoming project made this catch my eye), but sounds like what you need to do send the Cookie: param=value header (perhaps using the HTTP::Request module) for each parameter the server is setting/expecting a cookie value to be returned. Otherwise, the page you are going to is trying to set that value.

    You might also refer to Clinton Wong's Web Client Programming with Perl (although for some reason I can no longer seem to find it mentioned on O'Reilly's website.

    I would be very interested in how you resolve this, and I hope monks of more experience than I who have worked with this will chime in so we both may drink of the wisdom of how to accomplish this.