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

What I want is just to get a file from a fixed address:
use strict; use Net::HTTP; my $addr='www3.oup.co.uk'; my $file='/nar/database/cap'; my $IO=Net::HTTP->new('Host'=>$addr) || die $@; $IO->write_request(GET=>$file); my ($code,$message,@headers)=$IO->read_response_headers;
But what I get is only a "OK" in the $message, so where is the html file content?

Replies are listed 'Best First'.
Re: simple use of Net::HTTP
by ikegami (Patriarch) on Sep 11, 2008 at 07:51 UTC

    Read the rest of the Synopsis.

    You're probably better using LWP, by the way.

    use LWP::UserAgent qw( ); my $ua = LWP::UserAgent->new(); my $response = $ua->get('http://www3.oup.co.uk/nar/database/cap'); if ($response->is_success) { print $response->decoded_content; # or whatever } else { die $response->status_line; }
Re: simple use of Net::HTTP
by moritz (Cardinal) on Sep 11, 2008 at 07:49 UTC
    The html file content is part of the body, not of the header. You're only retrieving the header from your object so far.

    The documentation has an example of how to retrieve the body.

    But do you really need such a low level interface? LWP::Simple or LWP::UserAgent are much more high level, and usually recommended.

Re: simple use of Net::HTTP
by Anonymous Monk on Sep 11, 2008 at 07:53 UTC
    From the synopsis
    use Net::HTTP; my $s = Net::HTTP->new(Host => "www.perl.com") || die $@; $s->write_request(GET => "/", 'User-Agent' => "Mozilla/5.0"); my($code, $mess, %h) = $s->read_response_headers; while (1) { my $buf; my $n = $s->read_entity_body($buf, 1024); die "read failed: $!" unless defined $n; last unless $n; print $buf; }
    That read_entity_body part is what actually reads the file content.

    I think its a better idea if you use WWW::Mechanize, its a higher level interface

    use WWW::Mechanize; my $ua=WWW::Mechanize->new(); $ua->get($url); $ua->save_content('filename.html') if $ua->success();
Re: simple use of Net::HTTP
by peter (Sexton) on Sep 11, 2008 at 14:26 UTC

    You could use LWP::Simple, it's much easier to use.

    use LWP::Simple; $content = get("http://example.com/");
    Peter Stuifzand