[Net-ssleay-devel] file uploads

MH netssleay at madmardy.com
Fri Aug 25 01:52:39 CEST 2006


Here is what I came up with.  I finally found the documentation for
HTTP::Request::Common, which has some help for those trying to perform file
uploads.  Thanks for your help. :)

#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common;

my $host = "host.com";
my $uri  = "/bla/receive_upload";
my $file = '/cygdrive/c/Documents and Settings/user/My Documents/image.jpg';
my $path = 'C:\\Documents and Settings\\user\\My Documents\\image.jpg';
my $type = "image/jpg";

my $req = HTTP::Request::Common::POST(
  "https://$host$uri",
  Content_Type => 'form-data',
  Content      => [
    string => "blabla",
    file1  => [$file, $path, 'Content-Type' => $type],
  ]
);

my $ck  = new HTTP::Cookies(hide_cookie2 => 1);
my %cookies = (
  cookie1 => "cook1",
  cookie2 => "cook2",
);
foreach my $cookie (sort keys %cookies) {
  $ck->set_cookie(0, $cookie, $cookies{$cookie}, "/", $host, 443, 1, 1, 45,
0)
}

my $ua  = new LWP::UserAgent(
  agent => "Micro\$oft IE 1.0",
  cookie_jar => $ck,
);

my $resp = $ua->request($req);
if ($resp->is_success) {
  print $resp->content;
} else {
  print STDERR $resp->status_line, "\n";
}
__END__ 

-----Original Message-----
From: Florian Ragwitz [mailto:rafl at debian.org] 
Sent: Thursday, August 24, 2006 12:42 PM
To: MH
Cc: net-ssleay-devel at lists.alioth.debian.org
Subject: Re: [Net-ssleay-devel] file uploads

On Thu, Aug 24, 2006 at 12:17:15PM -0600, MH wrote:
> This may be a common question, but is there a way to send file uploads 
> using Net::SSLeay?

Yes, that's easily possible. File uploads are usually done using HTTP POST.
The most straightforward way to do so can be found in the
documentation:

  my ($page, $response, %reply_headers) = post_https($host, $port,
		  $path, make_form($param => $value, $param2 => $value2)));


Nevertheless I tend to suggest another way which uses Net::SSLeay only for
the SSL stuff and other modules for the HTTP part.

For this simple case I'd even suggest to not use Net::SSLeay at all (at
least not directly). LWP and friends offer you all you need to do that. 

Create an LWP useragent, an HTTP::Request instance using an https url and
make the useragent do the request. It'll automatically use a Module which
implements SSL for the https part. It'll chose between Net::SSL (prefered
unless the other one is already loaded) or IO::Socket::SSL, which is based
on Net::SSLeay. You won't need to care about the internals here. Everything
will work pretty much out of the box if one of those modules is installed.


Regards,
Flo

--
BOFH excuse #140:
LBNC (luser brain not connected)




More information about the Net-ssleay-devel mailing list