Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

File upload with LWP::UserAgent

by PerlRob (Sexton)
on Jun 12, 2008 at 22:33 UTC ( [id://691811]=perlquestion: print w/replies, xml ) Need Help??

PerlRob has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm attempting to write a Perl script that can upload a file to a web server based on the following very simple file upload form (URL redacted):
<form enctype="multipart/form-data" action="http://example.com" method +="post"> <input type="file" size="50" name="file_0"/><br /><br /> <input type="submit" value="Attach File(s)" name="confirm"/> </form>

And here's my Perl code (URL, username, and password redacted):
use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common; my $userAgent = LWP::UserAgent->new(); my $request = HTTP::Request->new(POST 'http://example.com', Content_Ty +pe => 'multipart/form-data', Content => [file_0 => ['options2.txt']]) +; $request->authorization_basic('username', 'password'); my $response = $userAgent->request($request); print $response->error_as_HTML . "\n" if $response->is_error;

This gives me a 400 "URL missing" error from the web server. Quite frankly, I have no idea what I'm doing wrong. I'm not a Perl noob, but I've never had occasion to use LWP::UserAgent or HTTP::Request, so I'm a noob in that sense.

Any ideas what I'm doing wrong or how I can debug?

Replies are listed 'Best First'.
Re: File upload with LWP::UserAgent
by Joost (Canon) on Jun 12, 2008 at 23:08 UTC
    URL missing is not a webserver response, it signifies that you didn't pass the URL:
    my $request = HTTP::Request->new(POST 'http://example.com', Content_Ty +pe => 'multipart/form-data', Content => [file_0 => ['options2.txt']]) +;

    You're missing => after POST. Which probably explains the immediate error: POST 'http://..' is seen as a function call (from HTTP:::Request::Common) instead of a string.

    AFAIK the content should be added using the content method, not as an argument to the constructor (since constructor arguments are all parsed as headers. See HTTP::Request

    Anyway, using WWW::Mechanize will probably be easier and make your code a bit more robust.

      Thanks for suggesting WWW::Mechanize. I'm getting a little further, but I think my problem now is that I can't get cookies working. When I authenticate, the appropriate page is returned, but the next page I request prompts me to log in again. Here's my code:
      use HTTP::Cookies; use WWW::Mechanize; my $cj = HTTP::Cookies->new( autosave => 1, ignore_discard => 1 ); my $mech = WWW::Mechanize->new( cookie_jar => $cj ); $mech->get( 'http://example.com/login.action' ); if ($mech->success()) { $mech->submit_form( form_name => 'loginform', fields => { os_usern +ame => 'username', os_password => 'password' } ); } if ($mech->success()) { $mech->follow_link( text => 'Training'); # This is where I get red +irected to the login page again! }

      Am I using cookies improperly, or is there some other problem with my code?
        I figured it out. The login form has a checkbox for remembering your username/password (as login forms often do). I simply had to use the tick() method to make sure that box was checked so that I could receive the login cookie and access subsequent pages.

        Thanks again for suggesting WWW::Mechanize!
Re: File upload with LWP::UserAgent
by Anonymous Monk on Sep 16, 2008 at 14:28 UTC
    ... my $userAgent = LWP::UserAgent->new(); my $request = POST 'http://example.com', Content_Type => 'multipart/fo +rm-data', Content => [file_0 => ['options2.txt']]; $request->authorization_basic('username', 'password'); my $response = $userAgent->request($request); print $response->error_as_HTML . "\n" if $response->is_error;
    would be correct, POST() creates HTTP::Request Object, making a HTTP::Request Object out of a HTTP::Request Object is nonsense.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://691811]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found