Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

file not getting through post

by inblosam (Monk)
on Jul 02, 2002 at 07:28 UTC ( #178784=perlquestion: print w/replies, xml ) Need Help??

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

My LWP post works great, except for the file is not getting there. See anything wrong with my code? Am I passing the content_type in correctly? I checked the path to the file, and it is correct. I also tried to read up on HTTP::Request::Common, but that didn't help me very much. THANKS!

#!/usr/lib/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common; use HTTP::Cookies; use LWP::Simple; my $ua = LWP::UserAgent->new; $ua->agent( "Mozilla/4.5" ); $ua->cookie_jar(HTTP::Cookies->new(file => 'cookie_jar', autosave =>1) +); # configure LWP::UserAgent to follow redirects after POST push @{ $ua->requests_redirectable }, 'POST'; $ua->request(POST "http://www.somesite.com/login.cfm", { username =>"test", password =>"abc", submit =>"Submit" }); my $request = $ua->request(POST "http://www.somesite.com/editproduct.c +fm", Content_Type => 'multipart/form-data', Content => [ name =>"Name", zfile =>"$ENV{DOCUMENT_ROOT}/filena +me.zip", update =>"Update" ]); $b = $request->is_success ? "Application information updated successfu +lly.\n" : "Error during update of application information. The error +has been logged and will be reviewed.\n"; print $b;


Michael Jensen
michael at inshift.com
http://www.inshift.com

Replies are listed 'Best First'.
Re: file not getting through post
by amphiplex (Monk) on Jul 02, 2002 at 07:58 UTC
    Hi ! This should work with a small change:
    zfile =>["$ENV{DOCUMENT_ROOT}/filename.zip"],
    instead of
    zfile =>"$ENV{DOCUMENT_ROOT}/filename.zip",
    From perldoc HTTP::Request::Common:
    The POST method also supports the "multi­ part/form-data" content used for Form-based File Upload as specified in RFC 1867. You trigger this content format by specifying a content type of "'form-data'" as one of the request headers. If one of the values in the $form_ref is an array reference, then it is treated as a file part specification with the following interpretation: [ $file, $filename, Header => Value... ] The first value in the array ($file) is the name of a file to open. This file will be read and its content placed in the request. The routine will croak if the file can't be opened. Use an "undef" as $file value if you want to specify the content directly. The $filename is the filename to report in the request. If this value is undefined, then the basename of the $file will be used. You can specify an empty string as $filename if you don't want any filename in the request.

    ---- kurt
Re: file not getting through post
by crazyinsomniac (Prior) on Jul 02, 2002 at 08:14 UTC
    ############################# BEEP [ zfile =>"$ENV{DOCUMENT_ROOT}/filename.zip" ], ...
    How to RTFM

    perldoc HTTP::Request::Common

    POST $url, [$form_ref], [Header => Value,...] This works mostly like GET() with POST as the method, but this function also takes a second optional array or hash reference parameter ($form_ref). This argument can be used to pass key/v +alue pairs for the form content. By default we will initialize a re +quest using the "application/x-www-form-urlencoded" content type. Th +is means that you can emulate a HTML <form> POSTing like this: POST 'http://www.perl.org/survey.cgi', [ name => 'Gisle Aas', email => 'gisle@aas.no', gender => 'M', born => '1964', perc => '3%', ]; This will create a HTTP::Request object that looks like this: POST http://www.perl.org/survey.cgi Content-Length: 66 Content-Type: application/x-www-form-urlencoded name=Gisle%20Aas&email=gisle%40aas.no&gender=M&born=1964&per +c=3%25 ############################# BEEP The POST method also supports the "multipart/form-data" conten +t used for *Form-based File Upload* as specified in RFC 1867. You tri +gger this content format by specifying a content type of "'form-dat +a'" as one of the request headers. If one of the values in the $form_ +ref is an array reference, then it is treated as a file part specific +ation with the following interpretation: [ $file, $filename, Header => Value... ] The first value in the array ($file) is the name of a file to +open. This file will be read and its content placed in the request. +The routine will croak if the file can't be opened. Use an "undef" + as $file value if you want to specify the content directly. The $filename is the filename to report in the request. If this va +lue is undefined, then the basename of the $file will be used. You ca +n specify an empty string as $filename if you don't want any fil +ename in the request. Sending my ~/.profile to the survey used as example above can +be achieved by this: POST 'http://www.perl.org/survey.cgi', Content_Type => 'form-data', Content => [ name => 'Gisle Aas', email => 'gisle@aas.no', gender => 'M', born => '1964', ############################# BEEP init => ["$ENV{HOME}/.profile"], ] This will create a HTTP::Request object that almost looks this + (the boundary and the content of your ~/.profile is likely to be different): POST http://www.perl.org/survey.cgi Content-Length: 388 Content-Type: multipart/form-data; boundary="6G+f" --6G+f Content-Disposition: form-data; name="name" Gisle Aas --6G+f Content-Disposition: form-data; name="email" gisle@aas.no --6G+f Content-Disposition: form-data; name="gender" M --6G+f Content-Disposition: form-data; name="born" 1964 --6G+f Content-Disposition: form-data; name="init"; filename=".prof +ile" Content-Type: text/plain PATH=/local/perl/bin:$PATH export PATH --6G+f-- If you set the $DYNAMIC_FILE_UPLOAD variable (exportable) to s +ome TRUE value, then you get back a request object with a subrouti +ne closure as the content attribute. This subroutine will read th +e content of any files on demand and return it in suitable chunk +s. This allow you to upload arbitrary big files without using lot +s of memory. You can even upload infinite files like /dev/audio if +you wish; however, if the file is not a plain file, there will be +no Content-Length header defined for the request. Not all servers + (or server applications) like this. Also, if the file(s) change in + size between the time the Content-Length is calculated and the time + that the last chunk is delivered, the subroutine will "Croak".

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2019-10-19 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?