in reply to Re: Re: LWP head mystery
in thread LWP head mystery
HEAD is useful when you want information about a page, but don't actually want to see the page itself. It's most useful to check if a page exists, or when it was last modified. The biggest users of HEAD requests are proxies, which use HEAD requests to check whether or not they have a current copy of the page in their cache. If the last-modified date returned in the HEAD matches what the proxy has cached, then a whole page lookup is saved.
To access the content-type and get the content at the same time, you'll want to look at using LWP::UserAgent. In particular, the responses you get back from request method are HTTP::Response methods. You can call ->headers or ->headers_as_string to get back the headers (including content-type) as either a HTTP::Headers or as a string respectively.
A bit of sample code may help. This demonstrates how to pull back the frontpage of perlmonks.org, and prints the content type and the content retrieved.
Cheers,use LWP::UserAgent; use HTTP::Request::Common; my $ua = LWP::UserAgent->new; my $response = $ua->request(GET "http://perlmonks.org/"); print "Content type is ", $response->headers->header("Content-Type"), "\n----\nContent is \n", $response->content;
Paul
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: When to use HEAD vs GET
by Anonymous Monk on May 06, 2014 at 09:15 UTC |