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

Wise monks,
I am trying to get a file up on a Puppet server, which supports a REST API briefly documented here: docs.puppetlabs.com
To GET a file, I must pass a header of "Accept: s" and just slurp it in a string
$ua = LWP::UserAgent->new(); $as = HTTP::Headers->new; $as->header('Accept' => 's'); $req = HTTP::Request->new('GET', "https://$server/production/file_buck +et_file/md5/$md5", $as); $res = $ua->request($req); return $res->content;
It's not quite clear how LWP PUTs files:
open my $up, "<", "$_[0]" or die "Something went wrong: ".$!; binmode +$up; my $ck = Digest::MD5->new; $ck->addfile($up); my $req = HTTP::Request->new('PUT', "https://".$server."/file_bucket_f +ile/md5/".$_[1]."/".$ck->hexdigest, $as, ...?); my $res = $ua->request($req); close $up; die "Something went wrong: ".$res->status_line unless $res->is_success +;
The md5 part is something required by Puppet, nothing to do with LWP itself

Replies are listed 'Best First'.
Re: Upload file to REST service
by Anonymous Monk on May 31, 2011 at 23:27 UTC
      Something is wrong with the syntax though, and I can't understand what:
      open my $up, "<", "$_[0]" or die "Something went wrong: ".$!; binmode +$up; my $ck = Digest::MD5->new; $ck->addfile($up); close $up; my $us = HTTP::Headers->new; $us->header(Content_Type => 'multipart/fo +rm-data'); my $req = HTTP::Request->new('POST',"https://".$server."/file_bucket_f +ile/md5/".$_[1]."/".$ck->hexdigest, $us, [ file => ["$_[0]"] ]); print Dumper($req); my $res = $ua->request($req); die "Something went wrong: ".$res->status_line unless $res->is_success +;
      perl -wc file doesn't complain, but when running I get "Can't call method "request" on an undefined value ". $req is defined, and shown on the line above, but the code doesn't get to the "something went wrong" line.
      I have tried various invocations (there are probably 50 ways in which one can call LWP::UserAgent). with and without HTTP::Request::Common::DYNAMIC_FILE_UPLOAD, no luck..
        Something is wrong with the syntax though, and I can't understand

        Nothing is wrong with the syntax, there are no syntax errors

        get "Can't call method "request" on an undefined value ". $req is defined, and shown on the line above,

        But you aren't calling any method request on variable $req. See diagnostics