Message

UTF-8

">

NAME

HTTP::Message - class encapsulating HTTP message

SYNOPSIS

use HTTP::Message;
    my $raw_msg = "GET / HTTP/1.1\r\nHost: somehost\r\n\r\n";
    my $mess = HTTP::Message.new.parse($raw_msg);
    say $mess;

DESCRIPTION

This module provides a bunch of methods to easily manage HTTP message.

METHODS

method new

method new($content?, *%fields)

A constructor, takes following parameters:

  • content : content of the message (optional)

  • fields : fields of the header section

my $msg = HTTP::Message.new('content', :field<value>);

method add-content

method add-content(HTTP::Message:, Str $content)

Adds HTTP message content. It does not remove the existing value, it concats to the existing content.

my $msg = HTTP::Message.new('content', :field<value>);
    $msg.add-content: 's';
    say $msg.content; # says 'contents'

method decoded-content

method decoded-content(HTTP::Message:)

Returns decoded content of the message (using Encode module to decode).

my $msg = HTTP::Message.new();
    say $msg.decoded-content;

method field

multi method field(HTTP::Message:, Str $s) returns HTTP::Header::Field
    multi method field(HTTP::Message:, *%fields)

See HTTP::Header.

method init-field

method init-field(HTTP::Message:, *%fields)

See HTTP::Header.

method push-field

method push-field(HTTP::Message:, HTTP::Header::Field $field)

See HTTP::Header.

method remove-field

method remove-field(HTTP::Message:, Str $field)

See HTTP::Header.

method clear

method clear(HTTP::Message:)

Removes the whole message, both header and content section.

my $msg = HTTP::Message.new('content', :field<value>);
    $msg.clear;
    say ~$msg; # says nothing

method parse

method parse(HTTP::Message:, Str $raw_message) returns HTTP::Message

Parses the whole HTTP message.

It takes the HTTP message (with \r\n as a line separator) and obtain the header and content section, creates a HTTP::Header object.

my $msg = HTTP::Message.new.parse("GET / HTTP/1.1\r\nHost: example\r\ncontent\r\n");
    say $msg.perl;

method Str

method Str(HTTP::Message:, Str $eol = "\n") returns Str

Returns HTTP message in a readable form.

SEE ALSO

HTTP::Request, HTTP::Response, Encode

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.