Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

File POSTing with WWW::Curl::Easy

by kurt2439 (Sexton)
on Jan 26, 2012 at 14:00 UTC ( #950090=perlquestion: print w/replies, xml ) Need Help??
kurt2439 has asked for the wisdom of the Perl Monks concerning the following question:

I know this isn't a perl loved library and most people use LWP but I've found myself using it for a project. I'm trying to get a file upload to work along with submitting a bunch of other data during my POST.

I've found a bunch of write-ups on this but none seem to work for me. Many say to format the file postfield as "attachment"="@/file/path/file.ext" where the '@' sign signifies to cURL that this is a file upload and it must do some cURL magic. This didn't work for me but perhaps there is a setopt command that must be enabled on the cURL handle? Anyone have any experience with this?

here is a bit of relevant code (sorry it won't be helpful to you to run since it is logging into an authenticated system and using a proprietary module for post)

sub post_to_hsuforum{ my ( $self, $user_id, $course_id, $forum_id, $post_subject, $post_ +message ) = @_; my $hsu_forums_post_path="/mod/hsuforum/post.php"; my $post_url="https://$self->{'fqdn'}$hsu_forums_post_path"; my $max_file_size="10485760"; my $timestart="0"; my $timeend="0"; my $reveal="0"; my $show_anony="0"; my $course="$course_id"; my $hsuforum="$forum_id"; my $discussion="0"; my $parent="0"; my $userid="$user_id"; my $edit="0"; my $reply="0"; my $sesskey="$self->{sesskey}"; my $_qf__mod_hsuforum_post_form="1"; my $subject="$post_subject"; my $message="$post_message"; my $format="1"; my $subscribe="0"; my $attachment="@/home/jchase/test.txt"; my $submitbutton="Post to forum"; my $post_field="MAX_FILE_SIZE=$max_file_size&timestart=$timestart& +timeend=$timeend&reveal=$reveal&show_anony=$show_anony" ."&course=$course&hsuforum=$hsuforum&discussion=$discussion&pa +rent=$parent&userid=$userid&groupid=&edit=$edit" ."&reply=$reply&privatereply=&sesskey=$sesskey&_qf__mod_hsufor +um_post_form=$_qf__mod_hsuforum_post_form&subject=$subject" ."&message=$message&format=$format&subscribe=$subscribe&attach +ment=$attachment&submitbutton=$submitbutton"; curl_functions::post_submit( $self->{'connection_handle'}, $post_u +rl, $post_field ); }

Here is my post submission code

sub post_submit{ my $ch=shift; # Curl handle my $url=shift; # Url to submit form my $postfield=shift; # POST variables to submit #SET URL FOR THE POST FORM LOGIN $ch->setopt(CURLOPT_URL, "$url"); #ENABLE HTTP POST $ch->setopt(CURLOPT_POST, 1); #SET POST PARAMETERS : FORM VALUES FOR EACH FIELD $ch->setopt(CURLOPT_POSTFIELDS, "$postfield"); my $tmp_file='/tmp/post_response.txt'; open(my $write_responsefile,'>',"$tmp_file") or die "Could not ope +n $tmp_file for write"; $ch->setopt(CURLOPT_FILE, $write_responsefile); #EXECUTE REQUEST my $retval = $ch->perform; close($write_responsefile); if ($retval){ return 0; }else{ return 1; } }

Replies are listed 'Best First'.
Re: File POSTing with WWW::Curl::Easy
by Anonymous Monk on Jan 26, 2012 at 15:07 UTC

      You're right -- the WWW::Curl::Form made this super easy after I realized that the package created the linked list object to include as an argument to the CURLOPT_HTTPPOST in your curl handle and was NOT a separate, limited curl handle object (I know, I know, it was right there in the code example but it was so easy to overlook). It does require a newer version of WWW::Curl than ship with CentOS5 just for reference for others. Learned a bunch about the different POST options sludging through the pages too

      Thanks for the links and the help. Guess I should have done a better job searching here. I shall do penance

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950090]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-04-24 19:06 GMT
Find Nodes?
    Voting Booth?