See attached patch for HTTP-Lite (against version 2.3).
This allows the following use:
#Create http object
my $http = HTTP::Lite->new;
#Set the content type
$http->add_req_header('Content-Type', 'application/octet-stream');
#Set the request method
$http->method('POST');
#Set the file that has to be uploaded
$http->{'content'} = {'file' => <FILENAME>};
#Make the request
$http->request(<URL>);
This code will send a 'POST' request to <URL> and the file <FILENAME>
will be streamed as the body of the request.
Index: perl5/HTTP/Lite.pm
===================================================================
--- perl5/HTTP/Lite.pm (revision 21952)
+++ perl5/HTTP/Lite.pm (working copy)
@@ -219,9 +219,10 @@
if (!defined($self->get_req_header("Accept"))) {
$self->add_req_header("Accept", "*/*");
}
-
- if ($method eq 'POST') {
- $self->http_write(*FH, "Content-Type: application/x-www-form-urlencoded$CRLF");
+ my $content_type =
+ $self->get_req_header("Content-Type") || $method eq 'POST' && 'application/x-www-form-urlencoded';
+ if ($content_type) {
+ $self->http_write(*FH, "Content-Type: $content_type$CRLF");
}
# Purge a couple others
@@ -238,7 +239,11 @@
my $content_length;
if (defined($self->{content}))
{
- $content_length = length($self->{content});
+ if (!ref $self->{content}) {
+ $content_length = length($self->{content});
+ } elsif (ref $self->{content} eq 'HASH' && defined $self->{content}->{'file'}) {
+ $content_length = -s $self->{content}->{'file'};
+ }
}
if (defined($callback_func)) {
my $ncontent_length = &$callback_func($self, "content-length", undef, @$callback_params);
@@ -271,9 +276,25 @@
}
# Output content, if any
- if (!$content_out && defined($self->{content}))
- {
- $self->http_write(*FH, $self->{content});
+ if (!$content_out && defined($self->{content})) {
+ if (!ref $self->{content}) {
+ $self->http_write(*FH, $self->{content});
+ } elsif (
+ ref $self->{content} eq 'HASH' &&
+ defined $self->{content}->{'file'} &&
+ -r $self->{content}->{'file'}
+ ) {
+ local *FILE;
+ my $data;
+
+ if (open(FILE, '<', $self->{content}->{'file'})) {
+ binmode(FILE);
+ while (read(FILE, $data, 1024)) {
+ $self->http_write(*FH, $data);
+ }
+ close(FILE);
+ }
+ }
}
if (defined($callback_func)) {