Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

grab HTTP Response and Headers

by agent00013 (Pilgrim)
on Jul 12, 2001 at 18:23 UTC ( [id://96037]=CUFP: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://96037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-26 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found