Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: XPOST syntax in WWW::Curl

by Steve_BZ (Chaplain)
on Feb 02, 2015 at 19:08 UTC ( [id://1115316]=note: print w/replies, xml ) Need Help??


in reply to XPOST syntax in WWW::Curl

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1115316]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-06-20 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.