For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > May 2004 > 400 Bad Request using LWP::UserAgent and POST









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author 400 Bad Request using LWP::UserAgent and POST
Gunnar Strand

2004-05-26, 5:31 pm

Hi,

I am working on a script to automatically upload files to my
account on Yahoo. It logs in, retrieves the form for uploading
files, but when it posts the server returns '400 Bad Request'
(HTTP syntax error). LWP::UserAgent handles all traffic to
get cookies right. I have tried turning on LWP::Debug, but
all I can see is that the UserAgent POST line differs from
a successful POST by my browser (I've edited the Y! user name):

UserAgent:
LWP::UserAgent::send_request: POST
http://f1.up.briefcase.yahoo.com/ed...cmultipart_form

Mozilla:
POST /edit/myuser/process_bcmultipart_form HTTP/1.1

Too bad that LWP does not trace all communication. It would
have been good to compare it all. Here is the perl code which
does the upload:

sub UploadFiles ( @ ) {
my ( @files ) = @_;

my $f;
while ( $f = shift @files ) {
next unless -f $f;

$verbose && print "Fetching file upload form\n";

my $res = $ua -> get(
'http://f1.up.briefcase.yahoo.com/edit/myuser/fupload_form?.dir=/BAR&.src=bc&.action=upload&.done=http%3a//f1.pg.briefcase.yahoo.com/bc/myuser/lst%3f%26.dir=/BAR%26.src=bc%26.view=l&.mesg='
);
unless ( 200 == $res -> code() ) {
die $res -> status_line();
}

$verbose && print "Uploading $f\n";

my $form = HTML::Form -> parse( $res );
foreach my $i ( 1..5 ) {
$form -> value( "file$i", '' );
}
$form -> value( 'file0', $f );
#my $req = $form -> make_request(); <- does not work
my $req = $form -> click( '.upload' );
$res = $ua -> request( $req );
unless ( 200 == $res -> code() ||
302 == $res -> code() ) {
die $res -> status_line(); # line 76
}

#unlink $f;
}
}

And here's the output:
Fetching file upload form
Uploading /tmp/upload_spool/a.txt
400 Bad Request at ./uploader.pl line 76.

I have tried printing the contents of the request object and that
looks fine compared to the data Mozilla sends. I get the impression
that there is something fishy about the headers, or maybe I am just
paranoid about things I don't see.

Kind Regards,

/Gunnar
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com