This snippet grabs the HTTP response and headers of a URL sent in through standard input.

Usage: scriptname URL

Update: As $code_or_die pointed out, this grabs the full response along with HTTP headers. This is the way I wanted it to work, so I've made this more clear in the title and description.
#!/usr/local/bin/perl -w #turn on warnings use strict; #always use use LWP::UserAgent; #used for the request use HTTP::Request::Common; #used to build request my ($link,$ua,$req,$results); #declare variables $link = shift; #grab link from standard input $ua = LWP::UserAgent->new; #create new user agent $req = HTTP::Request->new(HEAD => $link);#grab the header $results = $ua->request($req)->as_string; #grab the results print "$results"; #print the header info

Replies are listed 'Best First'.
Re: grab HTTP headers
by $code or die (Deacon) on Jul 12, 2001 at 18:37 UTC
    I think you want to replace:
    $results = $ua->request($req)->as_string; #grab the results
    with:
    $results = $ua->request($req)->headers_as_string; #grab the results
    When I ran your snippet, it printed out the whole response, not just the headers.

    update: I used this in the past:
    use strict; use LWP::Simple qw(get head); my $url = 'http://www.w3c.org'; print head($url)->{_headers}->as_string;


    Error: Keyboard not attached. Press F1 to continue.