Search code examples
perlhttpcookieslwp

How can I send cookies with Perl's LWP::Simple?


use LWP::Simple;
use HTML::LinkExtor;
use Data::Dumper;
#my $url = shift @ARGV;
my $content = get('example.com?GET=whateverIwant');
my $parser = HTML::LinkExtor->new(); #create LinkExtor object with no callbacks
$parser->parse($content); #parse content

now if I want to send POST and COOKIE info as well with the HTTP header how can I configure that with the get funciton? or do I have to customize my own method?

My main interest is Cookies! then Post!


Solution

  • LWP::Simple is for very simple HTTP GET requests. If you need to do anything more complex (like cookies), you have to upgrade to a full LWP::UserAgent. The cookie_jar is a HTTP::Cookies object, and you can use its set_cookie method to add a cookie.

    use LWP::UserAgent;
    
    my $ua = LWP::UserAgent->new(cookie_jar => {}); # create an empty cookie jar
    
    $ua->cookie_jar->set_cookie(...);
    
    my $rsp = $ua->get('example.com?GET=whateverIwant');
    die $rsp->status_line unless $rsp->is_success;
    my $content = $rsp->decoded_content;
    ...
    

    The LWP::UserAgent also has a post method.