kiz has asked for the wisdom of the Perl Monks concerning the following question:
This works, but I get the following dump at the end:#!/path/to/bin/perl -w use strict; use AnyEvent::HTTP; use Data::Dumper; # CONF my @targets = qw(place1 place2 place3 place4 place5); my ($host, $username, $password, $collection) = (); my $endpoints = {}; ## deleted fragment that sets up end-point details ## ... basic format: ##$endpoints->{'place'}->{'host'} = 'my-host:port'; ##$endpoints->{'place'}->{'path'} = '/some/path'; ##$endpoints->{'place'}->{'username'} = 'u243'; ##$endpoints->{'place'}->{'password'} = 'divedivedive'; # content to send via POST: my $file = "broker_deposit.zip"; # An AnyEvent method to send a zip file to a sword endpoint # sub transfer($) { my ($params) = @_; my $cb = AnyEvent->condvar; my ($file, $host, $collection, $username, $password, $protocol); $file = $params->{file} if exists $params->{file}; $host = $params->{host} if exists $params->{host}; $protocol = $params->{protocol} if exists $params->{protocol}; $collection = $params->{collection} if exists $params->{collection +}; $username = $params->{username} if exists $params->{username}; $password = $params->{password} if exists $params->{password}; print "open file...."; # Load the file. If there is no file, then return from the event w +ith nothing my $archive = ""; open(FILE, $file) or return $cb->send("Unable to create Transfer package"); binmode FILE; while (my $l = <FILE>) { $archive .= $l; } close FILE; # Tell SWORD to process the contents of the zip file as the new OA +-RJ type my %headers = ( 'X-Packaging' => 'http://opendepot.org/broker/1.0', 'X-No-Op' => 'false', 'X-Verbose' => 'false', 'Content-Disposition' => "filename=$file", 'Content-Type' => 'application/zip', 'User::Agent' => 'OA-RJ Broker v0.2', ); print " make http request (http://${username}:${password}\@${host}${co +llection}:\n"; # this is a long call, interspersed with comments! http_post( # the main request we want to make "http://${username}:${password}\@${host}${collection}", # the body of the http_request $archive, # the headers for that request headers => \%headers, # What we do whilst we wait sub { my ($body, $header) = @_; my $status = $header->{Status}; if ($status =~ /20[01234]/ ) { # download ok || resume ok || file already fully downloade +d $cb->send($body); } else { $cb->send( Dumper($header) ); } } ); return $cb; } ## end sub transfer($$) my @responses; my $cv = AnyEvent->condvar; foreach my $target (@targets) { $cv->begin; print "$target: "; my $host = $endpoints->{$target}->{'host'} if exists $endpoints->{$target}->{'host'}; my $collection = $endpoints->{$target}->{'collection'} if exists $endpoints->{$target}->{'collection'}; my $username = $endpoints->{$target}->{'username'} if exists $endpoints->{$target}->{'username'}; $password = $endpoints->{$target}->{'password'} if exists $endpoints->{$target}->{'password'}; my %params = ( protocol => 'http', host => $host, collection => $collection, username => $username, password => $password, file => $zipfile, ); push @responses, transfer( \%params ); $cv->end; } ## end foreach my $target (@targets...) $cv->recv; print Dumper(\@responses); exit;
Fairly obviously, the problem is the line push @responses, transfer( \%params ); - but how DO I get the response from each PUT into my list of responses?$VAR1 = [ bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ), bless( {}, 'AnyEvent::CondVar' ) ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AnyEvent::HTTP and getting data back from a post request
by Anonymous Monk on Jan 27, 2011 at 13:02 UTC | |
by Anonymous Monk on Jan 27, 2011 at 13:15 UTC | |
by kiz (Monk) on Jan 27, 2011 at 14:59 UTC | |
by Corion (Patriarch) on Jan 27, 2011 at 15:31 UTC | |
by Anonymous Monk on Jan 27, 2011 at 15:42 UTC | |
by kiz (Monk) on Jan 27, 2011 at 16:13 UTC | |
by kiz (Monk) on Jan 27, 2011 at 15:02 UTC |