Hi Guys,

After a day of trying playing around with every option I could find, I came up with the following. The nice thing is that it has progress reporting.

Thanks for everyone's help,

Regards

Steve.

#!/usr/bin/perl -w use strict; use warnings; use WWW::Curl::Easy; # set custom HTTP request header fields my $url = "http://mysite.org:8098/api/resultados/"; my $token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; my $filetype_to_upload="pdf"; my $file_to_upload="/path/to/file.$filetype_to_upload"; my $size = -s $file_to_upload; open( IN, $file_to_upload ) or die("Cannot open file - $file_to_upload +. $! \n"); my $curl = WWW::Curl::Easy->new or die $!; # Headers. my @request_header = (); #push (@request_header, "Accept-Encoding: gzip"); push (@request_header, "Content-type: multipart/form-data; boundary=fr +ontier"); push (@request_header, "Authorization: Token $token"); push (@request_header, "User-Agent: Perl interface for libcURL/7.35.0" +); # Options my $retcode ; $retcode = $curl->setopt(CURLOPT_POST, 1); $retcode = $curl->setopt(CURLOPT_VERBOSE,0); $retcode = $curl->setopt(CURLOPT_HEADER(), 1); $retcode = $curl->setopt(CURLOPT_HTTPHEADER, \@request_header); $retcode = $curl->setopt(CURLOPT_URL(), $url); $retcode = $curl->setopt(CURLOPT_NOPROGRESS, 0); $retcode = $curl->setopt(CURLOPT_PROGRESSFUNCTION, \&progress_callback +); $retcode = $curl->setopt( CURLOPT_INFILESIZE, $size ); $retcode = $curl->setopt( CURLOPT_UPLOAD, 1 ); $retcode = $curl->setopt( CURLOPT_CUSTOMREQUEST, 'POST' ); $retcode = $curl->setopt(CURLOPT_TIMEOUT, 30); $curl->setopt(CURLOPT_READDATA, \*IN); $curl->setopt( CURLOPT_POSTFIELDSIZE_LARGE, -1 ); $retcode = $curl->perform; if ($retcode == 0) { print("File upload success\n"); } else { print("An error happened: $retcode ".$curl->strerror($retcode)."\n +"); } exit; sub progress_callback { my ($clientp,$dltotal,$dlnow,$ultotal,$ulnow)=@_; if ($ultotal) { print int (100 * $ulnow/$ultotal), "% Complete.\n"; } else { print "0% Complete.\n"; } return 0; }

In reply to Re: XPOST syntax in WWW::Curl by Steve_BZ
in thread XPOST syntax in WWW::Curl by Steve_BZ

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.