#!/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=frontier"); 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; }