Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

XPOST syntax in WWW::Curl

by Steve_BZ (Chaplain)
on Feb 01, 2015 at 12:06 UTC ( [id://1115215]=perlquestion: print w/replies, xml ) Need Help??

Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys,

I'm trying to reprogram the following command with WWW::Curl. The command (which works as it is) has been provided by our site developer:

print STDOUT `curl -XPOST '$url' -H 'Authorization: Token $token'  -F $gl_media_defaults{v_suffix}='\@$file_to_upload'`;

I want to have more control over error handling and the progress of large uploads as some uploads can provide no feedback for several minutes.

As others have mentioned (CURL and Perl (it rymes!)), the doc is a bit hard to access without any knowledge of Curl.

I see other advice about using LWP::UserAgent, which I'm happy to follow, but the same point applies. I don't want to have to learn the whole of Curl or UserAgent, just to upload a file.

Thanks in advance.

Steve.

Replies are listed 'Best First'.
Re: XPOST syntax in WWW::Curl
by Corion (Patriarch) on Feb 01, 2015 at 13:34 UTC

    You will have to learn one of the two. Maybe HTTP::Request::Common is enough to produce a POST request, but you will have to learn HTTP in either case anyway.

    A very simplicistic approach could be to dump what curl sends over the network, and then replicate the request with LWP::UserAgent, printing that until you get the same results.

      Ok, nice. How do I find out what Curl sends over the network?

        Either use Wireshark or look at the Curl documentation. On a quick glance, either --verbose or --trace make Curl dump everything it sends or receives.

        Again, you will have to learn Curl and HTTP in either case, so now is a good time to start with that.

        As I now found out, I already said the same, even more verbose, in Re: CURL and Perl (it rhymes!). So maybe re-reading that thread helps.

        Did you read the thread you linked to in the OP?

        Cheers Rolf

        PS: Je suis Charlie!

Re: XPOST syntax in WWW::Curl
by pme (Monsignor) on Feb 01, 2015 at 15:57 UTC
    Hi Steve_BZ,

    This is a LWP::UserAgent based solution. The code snippet is untested and definitely needs modification to insert into your code environmant.

    use LWP::UserAgent; $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(POST => "$url"); # -X $req->header('Authorization' => "Token $token"); # -H my $res = $ua->request($req, [ $gl_media_defaults{v_suffix} => "\@$fil +e_to_upload"]); # -F if ($res->is_success) { print $res->content; } else { print $res->message; }

      Hi pme,

      Thanks for that.

      Well it seemed to work (that is to say it didn't error). I won't be able to see until tomorrow if the files were uploaded or not.

      The only thing that was suspicious is that the mp4 file was too quick to upload, so I imagine there was a problem.

      However, it gives me something to work on and debug. Thanks very much,

      Regards

      Steve.

Re: XPOST syntax in WWW::Curl
by Steve_BZ (Chaplain) on Feb 02, 2015 at 19:08 UTC

    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: perlquestion [id://1115215]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-16 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found