{
    "mode": "perldoc",
    "parameter": "HTTP::Headers",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/HTTP%3A%3AHeaders/json",
    "generated": "2026-06-13T22:59:17Z",
    "synopsis": "require HTTP::Headers;\n$h = HTTP::Headers->new;\n$h->header('Content-Type' => 'text/plain');  # set\n$ct = $h->header('Content-Type');            # get\n$h->removeheader('Content-Type');           # delete",
    "sections": {
        "NAME": {
            "content": "HTTP::Headers - Class encapsulating HTTP Message headers\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 6.36\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "require HTTP::Headers;\n$h = HTTP::Headers->new;\n\n$h->header('Content-Type' => 'text/plain');  # set\n$ct = $h->header('Content-Type');            # get\n$h->removeheader('Content-Type');           # delete\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"HTTP::Headers\" class encapsulates HTTP-style message headers. The headers consist of\nattribute-value pairs also called fields, which may be repeated, and which are printed in a\nparticular order. The field names are cases insensitive.\n\nInstances of this class are usually created as member variables of the \"HTTP::Request\" and\n\"HTTP::Response\" classes, internal to the library.\n\nThe following methods are available:\n\n$h = HTTP::Headers->new\nConstructs a new \"HTTP::Headers\" object. You might pass some initial attribute-value pairs\nas parameters to the constructor. *E.g.*:\n\n$h = HTTP::Headers->new(\nDate         => 'Thu, 03 Feb 1994 00:00:00 GMT',\nContentType => 'text/html; version=3.2',\nContentBase => 'http://www.perl.org/');\n\nThe constructor arguments are passed to the \"header\" method which is described below.\n\n$h->clone\nReturns a copy of this \"HTTP::Headers\" object.\n\n$h->header( $field )\n$h->header( $field => $value )\n$h->header( $f1 => $v1, $f2 => $v2, ... )\nGet or set the value of one or more header fields. The header field name ($field) is not\ncase sensitive. To make the life easier for perl users who wants to avoid quoting before the\n=> operator, you can use '' as a replacement for '-' in header names.\n\nThe header() method accepts multiple ($field => $value) pairs, which means that you can\nupdate several fields with a single invocation.\n\nThe $value argument may be a plain string or a reference to an array of strings for a\nmulti-valued field. If the $value is provided as \"undef\" then the field is removed. If the\n$value is not given, then that header field will remain unchanged. In addition to being a\nstring, $value may be something that stringifies.\n\nThe old value (or values) of the last of the header fields is returned. If no such field\nexists \"undef\" will be returned.\n\nA multi-valued field will be returned as separate values in list context and will be\nconcatenated with \", \" as separator in scalar context. The HTTP spec (RFC 2616) promises\nthat joining multiple values in this way will not change the semantic of a header field, but\nin practice there are cases like old-style Netscape cookies (see HTTP::Cookies) where \",\" is\nused as part of the syntax of a single field value.\n\nExamples:\n\n$header->header(MIMEVersion => '1.0',\nUserAgent   => 'My-Web-Client/0.01');\n$header->header(Accept => \"text/html, text/plain, image/*\");\n$header->header(Accept => [qw(text/html text/plain image/*)]);\n@accepts = $header->header('Accept');  # get multiple values\n$accepts = $header->header('Accept');  # get values as a single string\n\n$h->pushheader( $field => $value )\n$h->pushheader( $f1 => $v1, $f2 => $v2, ... )\nAdd a new field value for the specified header field. Previous values for the same field are\nretained.\n\nAs for the header() method, the field name ($field) is not case sensitive and '' can be\nused as a replacement for '-'.\n\nThe $value argument may be a scalar or a reference to a list of scalars.\n\n$header->pushheader(Accept => 'image/jpeg');\n$header->pushheader(Accept => [map \"image/$\", qw(gif png tiff)]);\n\n$h->initheader( $field => $value )\nSet the specified header to the given value, but only if no previous value for that field is\nset.\n\nThe header field name ($field) is not case sensitive and '' can be used as a replacement\nfor '-'.\n\nThe $value argument may be a scalar or a reference to a list of scalars.\n\n$h->removeheader( $field, ... )\nThis function removes the header fields with the specified names.\n\nThe header field names ($field) are not case sensitive and '' can be used as a replacement\nfor '-'.\n\nThe return value is the values of the fields removed. In scalar context the number of fields\nremoved is returned.\n\nNote that if you pass in multiple field names then it is generally not possible to tell\nwhich of the returned values belonged to which field.\n\n$h->removecontentheaders\nThis will remove all the header fields used to describe the content of a message. All header\nfield names prefixed with \"Content-\" fall into this category, as well as \"Allow\", \"Expires\"\nand \"Last-Modified\". RFC 2616 denotes these fields as *Entity Header Fields*.\n\nThe return value is a new \"HTTP::Headers\" object that contains the removed headers only.\n\n$h->clear\nThis will remove all header fields.\n\n$h->headerfieldnames\nReturns the list of distinct names for the fields present in the header. The field names\nhave case as suggested by HTTP spec, and the names are returned in the recommended \"Good\nPractice\" order.\n\nIn scalar context return the number of distinct field names.\n\n$h->scan( \\&processheaderfield )\nApply a subroutine to each header field in turn. The callback routine is called with two\nparameters; the name of the field and a single value (a string). If a header field is\nmulti-valued, then the routine is called once for each value. The field name passed to the\ncallback routine has case as suggested by HTTP spec, and the headers will be visited in the\nrecommended \"Good Practice\" order.\n\nAny return values of the callback routine are ignored. The loop can be broken by raising an\nexception (\"die\"), but the caller of scan() would have to trap the exception itself.\n\n$h->flatten()\nReturns the list of pairs of keys and values.\n\n$h->asstring\n$h->asstring( $eol )\nReturn the header fields as a formatted MIME header. Since it internally uses the \"scan\"\nmethod to build the string, the result will use case as suggested by HTTP spec, and it will\nfollow recommended \"Good Practice\" of ordering the header fields. Long header values are not\nfolded.\n\nThe optional $eol parameter specifies the line ending sequence to use. The default is \"\\n\".\nEmbedded \"\\n\" characters in header field values will be substituted with this line ending\nsequence.\n",
            "subsections": []
        },
        "CONVENIENCE METHODS": {
            "content": "The most frequently used headers can also be accessed through the following convenience methods.\nMost of these methods can both be used to read and to set the value of a header. The header\nvalue is set if you pass an argument to the method. The old header value is always returned. If\nthe given header did not exist then \"undef\" is returned.\n\nMethods that deal with dates/times always convert their value to system time (seconds since Jan\n1, 1970) and they also expect this kind of value when the header value is set.\n\n$h->date\nThis header represents the date and time at which the message was originated. *E.g.*:\n\n$h->date(time);  # set current date\n\n$h->expires\nThis header gives the date and time after which the entity should be considered stale.\n\n$h->ifmodifiedsince\n$h->ifunmodifiedsince\nThese header fields are used to make a request conditional. If the requested resource has\n(or has not) been modified since the time specified in this field, then the server will\nreturn a \"304 Not Modified\" response instead of the document itself.\n\n$h->lastmodified\nThis header indicates the date and time at which the resource was last modified. *E.g.*:\n\n# check if document is more than 1 hour old\nif (my $lastmod = $h->lastmodified) {\nif ($lastmod < time - 60*60) {\n...\n}\n}\n\n$h->contenttype\nThe Content-Type header field indicates the media type of the message content. *E.g.*:\n\n$h->contenttype('text/html');\n\nThe value returned will be converted to lower case, and potential parameters will be chopped\noff and returned as a separate value if in an array context. If there is no such header\nfield, then the empty string is returned. This makes it safe to do the following:\n\nif ($h->contenttype eq 'text/html') {\n# we enter this place even if the real header value happens to\n# be 'TEXT/HTML; version=3.0'\n...\n}\n\n$h->contenttypecharset\nReturns the upper-cased charset specified in the Content-Type header. In list context return\nthe lower-cased bare content type followed by the upper-cased charset. Both values will be\n\"undef\" if not specified in the header.\n\n$h->contentistext\nReturns TRUE if the Content-Type header field indicate that the content is textual.\n\n$h->contentishtml\nReturns TRUE if the Content-Type header field indicate that the content is some kind of HTML\n(including XHTML). This method can't be used to set Content-Type.\n\n$h->contentisxhtml\nReturns TRUE if the Content-Type header field indicate that the content is XHTML. This\nmethod can't be used to set Content-Type.\n\n$h->contentisxml\nReturns TRUE if the Content-Type header field indicate that the content is XML. This method\ncan't be used to set Content-Type.\n\n$h->contentencoding\nThe Content-Encoding header field is used as a modifier to the media type. When present, its\nvalue indicates what additional encoding mechanism has been applied to the resource.\n\n$h->contentlength\nA decimal number indicating the size in bytes of the message content.\n\n$h->contentlanguage\nThe natural language(s) of the intended audience for the message content. The value is one\nor more language tags as defined by RFC 1766. Eg. \"no\" for some kind of Norwegian and\n\"en-US\" for English the way it is written in the US.\n\n$h->title\nThe title of the document. In libwww-perl this header will be initialized automatically from\nthe <TITLE>...</TITLE> element of HTML documents. *This header is no longer part of the HTTP\nstandard.*\n\n$h->useragent\nThis header field is used in request messages and contains information about the user agent\noriginating the request. *E.g.*:\n\n$h->useragent('Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0)');\n\n$h->server\nThe server header field contains information about the software being used by the\noriginating server program handling the request.\n\n$h->from\nThis header should contain an Internet e-mail address for the human user who controls the\nrequesting user agent. The address should be machine-usable, as defined by RFC822. E.g.:\n\n$h->from('King Kong <king@kong.com>');\n\n*This header is no longer part of the HTTP standard.*\n\n$h->referer\nUsed to specify the address (URI) of the document from which the requested resource address\nwas obtained.\n\nThe \"Free On-line Dictionary of Computing\" as this to say about the word *referer*:\n\n<World-Wide Web> A misspelling of \"referrer\" which\nsomehow made it into the {HTTP} standard.  A given {web\npage}'s referer (sic) is the {URL} of whatever web page\ncontains the link that the user followed to the current\npage.  Most browsers pass this information as part of a\nrequest.\n\n(1998-10-19)\n\nBy popular demand \"referrer\" exists as an alias for this method so you can avoid this\nmisspelling in your programs and still send the right thing on the wire.\n\nWhen setting the referrer, this method removes the fragment from the given URI if it is\npresent, as mandated by RFC2616. Note that the removal does *not* happen automatically if\nusing the header(), pushheader() or initheader() methods to set the referrer.\n\n$h->wwwauthenticate\nThis header must be included as part of a \"401 Unauthorized\" response. The field value\nconsist of a challenge that indicates the authentication scheme and parameters applicable to\nthe requested URI.\n\n$h->proxyauthenticate\nThis header must be included in a \"407 Proxy Authentication Required\" response.\n\n$h->authorization\n$h->proxyauthorization\nA user agent that wishes to authenticate itself with a server or a proxy, may do so by\nincluding these headers.\n\n$h->authorizationbasic\nThis method is used to get or set an authorization header that use the \"Basic Authentication\nScheme\". In array context it will return two values; the user name and the password. In\nscalar context it will return *\"uname:password\"* as a single string value.\n\nWhen used to set the header value, it expects two arguments. *E.g.*:\n\n$h->authorizationbasic($uname, $password);\n\nThe method will croak if the $uname contains a colon ':'.\n\n$h->proxyauthorizationbasic\nSame as authorizationbasic() but will set the \"Proxy-Authorization\" header instead.\n",
            "subsections": []
        },
        "NON-CANONICALIZED FIELD NAMES": {
            "content": "The header field name spelling is normally canonicalized including the '' to '-' translation.\nThere are some application where this is not appropriate. Prefixing field names with ':' allow\nyou to force a specific spelling. For example if you really want a header field name to show up\nas \"foobar\" instead of \"Foo-Bar\", you might set it like this:\n\n$h->header(\":foobar\" => 1);\n\nThese field names are returned with the ':' intact for $h->headerfieldnames and the $h->scan\ncallback, but the colons do not show in $h->asstring.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Gisle Aas <gisle@activestate.com>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 1994 by Gisle Aas.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "HTTP::Headers - Class encapsulating HTTP Message headers",
    "flags": [],
    "examples": [],
    "see_also": []
}