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

Thanks everyone for your helpful responses. (I have now shed my cloak of anonymity - i.e. worked out how to register).

Ha! My very first post and it turned our to be off-topic!

But since we've accidentally wandered off, can someone explain a bit further? I thought that every http access started with a HEAD in order to get the content-type - which is why I was so trusting of Gisle Aas's advice. Clearly I was wrong. But where exactly have I strayed from the path of righteousness?

Replies are listed 'Best First'.
When to use HEAD vs GET
by pjf (Curate) on Oct 07, 2001 at 02:19 UTC
    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

      Hi, Thanks for the post. It's really gives the clear information and very useful. Thanks again