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

Hi Fellow Monks
I've been searching through the old nodes to solve my problem but can't seem to get an answer

my knowledge on HTTP is limited to 2 days
All I was told is to get a zip file through HTTP

I found the following code where I can retrieve a htm file through http but I can't find a way to fetch a file like a zip file through http and put it in a specific folder in the web server.
Please Help

my $req = HTTP::Request->new(GET => 'http://localhost/dbconnect/instru +ctions.htm'); $req->header(Accept => "text/html, */*;q=0.1"); my $res = $ua->reques +t($req); # Check the outcome of the response if ($res->is_success) { print HTTP $res->content; } else { print LOG $res->status_line, "\n"; }
PugSA

Replies are listed 'Best First'.
Re: Get a file with HTTP
by planetscape (Chancellor) on Oct 26, 2005 at 06:33 UTC
Re: Get a file with HTTP
by svenXY (Deacon) on Oct 26, 2005 at 06:38 UTC
    Hi,
    use LWP::Simple:
    perl -MLWP::Simple -e 'mirror("http://path/to/zipfile.zip", "zipfile.z +ip");'

    Regards,
    svenXY
Re: Get a file with HTTP
by Corion (Patriarch) on Oct 26, 2005 at 06:41 UTC

    There are two easier ways than using LWP::UserAgent if all you want to do is to download a file via HTTP:

    • Use wget:
      system(qw( wget -O ), $target_name, $url) == 0 or warn "Couldn't download '$url': $?/$!";
    • Use LWP::Simple (included in LWP):
      mirror( $url, $target_name );
Re: Get a file with HTTP
by radiantmatrix (Parson) on Oct 26, 2005 at 19:30 UTC

    The reason you aren't able to download a ZIP file with your code is this line:

    $req->header(Accept => "text/html, */*;q=0.1");

    This is malformed (see some documentation) in such a way that it is only letting you accept HTML documents. What you wanted to say was:

    $req->header(Accept => "*/*; q=0.1, text/html");

    Which means "I prefer HTML, I will take anything else if it is the best available after a 90% 'quality' hit".

    Speaking HTTP is not easy, especially if you are just starting out. Unless you really need to speak HTTP directly like that, you should use one of the LWP kit's modules (which can use HTTP and FTP and the like automagically). I strongly agree with the people who have suggested LWP::Simple for the task you mention.

    use LWP::Simple; #get ZIP file and store it locally as 'download.zip' getstore('http://localhost/testfile.zip', 'download.zip' ); #print HTML file to STDOUT my $content = get('http://localhost/dbconnect/instructions.htm'); if (defined $content) { # it worked! print $content,"\n"; } else { # there was a problem! warn 'Failed getting HTML file' }
    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
      Thank you all for the help
Re: Get a file with HTTP
by murugu (Curate) on Oct 26, 2005 at 06:41 UTC
    LWP::Simple

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: Get a file with HTTP
by Anonymous Monk on Oct 26, 2005 at 07:02 UTC
    The code you have pasted would work for a zip file also or for that matter any file.