cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Post examples in PERL

From: <man_at_tfhs.net>
Date: Mon, 18 Jul 2005 13:46:51 -0000

On Mon, Jul 18, 2005, Chris Leahy <chris_at_sunhaus.com.au> said:

> Hi All
> I'm looking for some good post examples in PERL.
> Simple post and multiform post.
> Anybody have any?
>
> Thanks
>
> Regards Chris

have always used LWP (libwwwperl) for this. the code is pretty slow, cause
most of the multi-part post stuff is done in perl, rather than C, but it
does work. we used it on ~70 'ethernet fax' machines sending about half a
million http file uploads over a two year period. we have since re-written
that code in C using libcurl, its faster and integrated better with the
rest of our app.

here is a snippet you can use, license: public domain. as usual, be sure
to restore $/ if you intend to continue processing, change url and names
of http vars to suit. you can cut out the proxy bit if you dont need that.
i have found so many proxies require auth now, libcurl is better at that
than LWP.

allan

=======================================

#!/usr/bin/perl
                                                                          
                                                 
use strict;
use LWP::UserAgent;
use LWP::Simple;
                                                                          
                                                 
undef $/;
our $VERSION = '1.0.0';
our $url = 'http://foo.com/flarp';
our $filename = $ARGV[0];
our $proxy = $ARGV[1];
our $var1 = 'baz';
                                                                          
                                                 
my $count = 0;
                                                                          
                                                 
while($count++ < 3){
                                                                          
                                                 
    my $ua = LWP::UserAgent->new(env_proxy => 0,
                                keep_alive => 1,
                                   timeout => 60);
    $ua->protocols_allowed( ['http'] );
    $ua->agent("foo $VERSION");
                                                                          
                                                 
    if(length $proxy){
      $ua->proxy('http', $proxy);
    }
                                                                          
                                                 
    my $res = $ua->post(
      $url,
      Content_Type => 'form-data',
      Content => [ file => [$filename], var1 => $var1 ]
    );
                                                                          
                                                 
    if ( $res->is_success() ){
      warn $res->content;
    }
    else{
      warn $res->error_as_HTML;
    }
                                                                          
                                                 
    warn 'could not send data file, sleeping 1';
    warn $res->error_as_HTML();
    sleep 1;
                                                                          
                                                 
}
Received on 2005-07-18