Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

When to use HEAD vs GET

by pjf (Curate)
on Oct 07, 2001 at 02:19 UTC ( #117247=note: print w/replies, xml ) Need Help??


in reply to Re: Re: LWP head mystery
in thread LWP head mystery

G'day Elliott, welcome to the monestary,

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.

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;
Cheers,

Paul

Replies are listed 'Best First'.
Re: When to use HEAD vs GET
by Anonymous Monk on May 06, 2014 at 09:15 UTC
    Hi, Thanks for the post. It's really gives the clear information and very useful. Thanks again

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://117247]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-12-16 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (447 votes). Check out past polls.

    Notices?