HTTP::Body

HTTP Body Parser for Perl

HTTP::Body parses chunks of HTTP POST data and supports the following content types:

Chunked transfer encoding is fully supported.

HTTP::Body is used by Catalyst, Dancer, Web::Simple, and other Perl web frameworks.

Installation

From CPAN

cpanm HTTP::Body

Or with the classic CPAN client:

cpan HTTP::Body

From Source

git clone https://github.com/libwww-perl/HTTP-Body.git
cd HTTP-Body
cpanm Dist::Zilla
dzil install

System Package Managers

Debian / Ubuntu:

apt-get install libhttp-body-perl

Fedora:

dnf install perl-HTTP-Body

Dependencies

Synopsis

use HTTP::Body;

my $body = HTTP::Body->new($content_type, $content_length);

while (my $chunk = read_some_data()) {
    $body->add($chunk);
}

my $params  = $body->param;        # hashref of parsed parameters
my $uploads = $body->upload;       # hashref of file uploads
my $raw     = $body->body;         # IO::Handle for raw body
my $order   = $body->param_order;  # arrayref of param keys in order

Parsing a Multipart Upload

use HTTP::Body;

my $body = HTTP::Body->new('multipart/form-data; boundary=xYzZY', $content_length);
$body->tmpdir('/var/tmp');
$body->cleanup(1);  # auto-delete temp files when done

while (read($input, my $buffer, 8192)) {
    $body->add($buffer);
}

for my $name (keys %{ $body->upload }) {
    my $upload = $body->upload->{$name};
    my $tempfile = $upload->{tempname};
    my $filename = $upload->{filename};
    # process the uploaded file...
}

Chunked Transfer Encoding

# Omit content_length to enable chunked mode
my $body = HTTP::Body->new($content_type);

while (my $chunk = get_next_chunk()) {
    $body->add($chunk);
}

# Access trailing headers (if any)
my $trailing = $body->trailing_headers;

Multipart Upload Configuration

Since version 1.23, temporary files for uploads use the .upload extension by default instead of preserving the original file extension. This behavior can be customized:

# Restore old behavior (use original file extension)
$HTTP::Body::MultiPart::file_temp_suffix = undef;

# Custom File::Temp template
$HTTP::Body::MultiPart::file_temp_template = 'uploadXXXXXX';

# Custom File::Temp parameters
%HTTP::Body::MultiPart::file_temp_parameters = (
    UNLINK => 0,
    DIR    => '/my/upload/dir',
);

API

| Method | Description | |---|---| | new($content_type, [$content_length]) | Create a new parser. Omit length for chunked mode. | | add($data) | Feed data to the parser. | | body | Raw body as IO::Handle. | | param | Hashref of parsed form parameters. | | param_order | Arrayref of parameter keys in submission order. | | upload | Hashref of file uploads. | | part_data | Hashref with full multipart metadata per field. | | content_type | The content type of the request. | | content_length | The content length, or -1 for chunked. | | chunked | Returns 1 if the request uses chunked encoding. | | trailing_headers | HTTP::Headers object of trailing headers (chunked). | | tmpdir([$path]) | Get/set the temp directory for uploads. | | cleanup([$bool]) | Enable auto-deletion of temp files at DESTROY. | | state | Current parser state. | | length | Bytes read so far. |

Support

Authors

Contributors

License

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