How do I access the HTTP Header of request in a CGI script?

前端 未结 3 490
粉色の甜心
粉色の甜心 2020-12-16 11:29

I\'ve used Perl a bit for small applications and test code, but I\'m new to networking and CGI.

I get how to make the header of a request (using CGI.pm and printing

3条回答
  •  情话喂你
    2020-12-16 12:18

    The CGI module has a http() function you can use to that purpose:

    #!/usr/bin/perl --
    use strict;
    use warnings;
    use CGI;
    
    my $q = CGI->new;
    my %headers = map { $_ => $q->http($_) } $q->http();
    
    print $q->header('text/plain');
    print "Got the following headers:\n";
    for my $header ( keys %headers ) {
        print "$header: $headers{$header}\n";
    }
    

    Try it out; the above gives me:

    $ curl http://localhost/test.cgi -H "HeaderAttribute: value"
    Got the following headers:
    HTTP_HEADERATTRIBUTE: value
    HTTP_ACCEPT: */*
    HTTP_HOST: localhost
    HTTP_USER_AGENT: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
    

提交回复
热议问题