LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   [perl] uploading a file using POST (http://www.linuxquestions.org/questions/programming-9/%5Bperl%5D-uploading-a-file-using-post-641672/)

s0l1dsnak3123 05-12-2008 12:33 PM

[perl] uploading a file using POST
 
hi there, i am trying to add a function to an IRC bot I am developing that allows me to upload graphs to an imagehost called picboost.com. It uses POST to allow you to upload pictures.

this is what i have so far:

Code:

use LWP::UserAgent;
use File::Slurp;
use warnings;
use strict;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $url = "http://www.picboost.com/upload";
my $picture = "/home/snak3/graph.png";
my %args;
my $field_name = "userfile";
my $buf ;
my $buf_ref = $args{'buf'} || \$buf ;


my $value = read_file( $picture , binmode => ':raw' , scalar_ref => 1 );

my $response = $ua->post( $url, $field_name => $value);

print $response->content;

when i do this, all i get is the HTML page saying that i didn't select anything to upload... what am i doing wrong?

thanks in advance,
s0l1dsnak3123

ps. I am not fussed with what modules are used to upload the file. If I am using the wrong ones, or if you have more experience with a different module that can achieve the same task, no problem :)

Su-Shee 05-12-2008 01:07 PM

As far as I remember, you'll have to set the correct content type header manually.

Check "perldoc HTTP::Request::Common" for "form-data"; it contains several examples.

s0l1dsnak3123 05-12-2008 03:38 PM

Thanks very much :D

This did the trick:

Code:

use LWP::UserAgent;
use File::Slurp;
use warnings;
use strict;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $url = "http://www.picboost.com/upload";
my $picture = "/home/snak3/graph.png";
my %args;
my $field_name = "userfile";
my $buf ;
my $buf_ref = $args{'buf'} || \$buf ;


my $value = read_file( $picture , binmode => ':raw' , scalar_ref => 1 );

my $response = $ua->post( $url,
                        Content_Type => 'form-data',
                        Content => [ $field_name => ["$picture"] ]
                        );
print $response->content;



All times are GMT -5. The time now is 05:03 PM.