How to send a cookie using Perl 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 information also with an HTTP header, how can I configure this using the get funciton function? or do i need to customize my own method?

My main interest is Cookies! then Post!

+4
source share
2 answers

LWP :: Simple are very simple HTTP GET requests. If you need to do something more complex (like cookies), you need to upgrade to the full LWP :: UserAgent . cookie_jar is HTTP :: Cookies , 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; ... 

LWP :: UserAgent also has a post method.

+4
source

Instead, you can use WWW :: Mechanize . It already glues together most of the things you want:

  use WWW::Mechanize; my $mech = WWW::Mechanize->new( cookie_jar => { ... } ); $mech->cookie_jar->set_cookie(...); $mech->get( ... ); my @links = $mech->links; 
+2
source

All Articles