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; } }

In reply to File POSTing with WWW::Curl::Easy by kurt2439

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.