LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2008, 11:33 AM   #1
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Rep: Reputation: 0
[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
 
Old 05-12-2008, 12:07 PM   #2
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 510

Rep: Reputation: 53
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.
 
Old 05-12-2008, 02:38 PM   #3
s0l1dsnak3123
LQ Newbie
 
Registered: Jan 2008
Distribution: Ubuntu Hardy Heron
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks very much

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;
 
  


Reply

Tags
perl, post, upload



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
uploading file with POST from PHP issue lukebeales Programming 2 04-23-2008 08:54 PM
Uploading files to webserver in perl ? zulfilee Linux - Software 2 02-07-2005 11:07 PM
PHP and File Uploading Tomasfuego Linux - Software 6 08-12-2004 09:20 AM
uploading a file and cant resume skog Slackware 0 03-08-2004 10:11 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration