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×tart=$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 | |
by kurt2439 (Sexton) on Jan 27, 2012 at 15:10 UTC |