++ed by:

80 PAUSE users
64 non-PAUSE users.

Gisle Aas


HTTP::Message - HTTP style message (base class)


 use base 'HTTP::Message';


An HTTP::Message object contains some headers and a content body. The following methods are available:

$mess = HTTP::Message->new
$mess = HTTP::Message->new( $headers )
$mess = HTTP::Message->new( $headers, $content )

This constructs a new message object. Normally you would want construct HTTP::Request or HTTP::Response objects instead.

The optional $header argument should be a reference to an HTTP::Headers object or a plain array reference of key/value pairs. If an HTTP::Headers object is provided then a copy of it will be embedded into the constructed message, i.e. it will not be owned and can be modified afterwards without affecting the message.

The optional $content argument should be a string of bytes.

$mess = HTTP::Message->parse( $str )

This constructs a new message object by parsing the given string.


Returns the embedded HTTP::Headers object.

$mess->headers_as_string( $eol )

Call the as_string() method for the headers in the message. This will be the same as


but it will make your program a whole character shorter :-)

$mess->content( $content )

The content() method sets the content if an argument is given. If no argument is given the content is not touched. In either case the original content is returned.

Note that the content should be a string of bytes. Strings in perl can contain characters outside the range of a byte. The Encode module can be used to turn such strings into a string of bytes.

$mess->add_content( $data )

The add_content() methods appends more data to the end of the current content buffer.


The content_ref() method will return a reference to content buffer string. It can be more efficient to access the content this way if the content is huge, and it can even be used for direct manipulation of the content, for instance:

  ${$res->content_ref} =~ s/\bfoo\b/bar/g;

This example would modify the content buffer in-place.

$mess->parts( @parts )
$mess->parts( \@parts )

Messages can be composite, i.e. contain other messages. The composite messages have a content type of multipart/* or message/*. This method give access to the contained messages.

The argumentless form will return a list of HTTP::Message objects. If the content type of $msg is not multipart/* or message/* then this will return the empty list. In scalar context only the first object is returned. The returned message parts should be regarded as are read only (future versions of this library might make it possible to modify the parent by modifying the parts).

If the content type of $msg is message/* then there will only be one part returned.

If the content type is message/http, then the return value will be either an HTTP::Request or an HTTP::Response object.

If an @parts argument is given, then the content of the message will modified. The array reference form is provided so that an empty list can be provided. The @parts array should contain HTTP::Message objects. The @parts objects are owned by $mess after this call and should not be modified or made part of other messages.

When updating the message with this method and the old content type of $mess is not multipart/* or message/*, then the content type is set to multipart/mixed and all other content headers are cleared.

This method will croak if the content type is message/* and more than one part is provided.

$mess->add_part( $part )

This will add a part to a message. The $part argument should be another HTTP::Message object. If the previous content type of $mess is not multipart/* then the old content (together with all content headers) will be made part #1 and the content type made multipart/mixed before the new part is added. The $part object is owned by $mess after this call and should not be modified or made part of other messages.

There is no return value.


Will clear the headers and set the content to the empty string. There is no return value

$mess->protocol( $proto )

Sets the HTTP protocol used for the message. The protocol() is a string like HTTP/1.0 or HTTP/1.1.


Returns a copy of the message object.

$mess->as_string( $eol )

Returns the message formatted as a single string.

The optional $eol parameter specifies the line ending sequence to use. The default is "\n". If no $eol is given then as_string will ensure that the returned string is newline terminated (even when the message content is not). No extra newline is appended if an explicit $eol is passed.

All methods unknown to HTTP::Message itself are delegated to the HTTP::Headers object that is part of every message. This allows convenient access to these methods. Refer to HTTP::Headers for details of these methods:

    $mess->header( $field => $val )
    $mess->push_header( $field => $val )
    $mess->init_header( $field => $val )
    $mess->remove_header( $field )
    $mess->scan( \&doit )



Copyright 1995-2004 Gisle Aas.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.