in reply to Re: AnyEvent::HTTP and getting data back from a post request
in thread AnyEvent::HTTP and getting data back from a post request

Basically, like AnyEvent::HTTP says, http_ returns a guard variable, not the return value of the callback, so you have to write this
sub transfer($){ my $toReturn ; ... http_ ... sub { my( $body, $headers) = @_; $toReturn = [ $body, $headers ]; }; ... return $toREturn; }
The callback is only called at the end or on error, not whilst we wait, so that comment doesn't match the code

Replies are listed 'Best First'.
Re^3: AnyEvent::HTTP and getting data back from a post request
by kiz (Monk) on Jan 27, 2011 at 14:59 UTC
    Thanks for that.... I've poked at the code some more:
    my $myReturn; http_post( http://${username}:${password}\@${host}${collection}", $archive, headers => \%headers, sub { my ($body, $header) = @_; print "web response: ".Dumper([$body, $header]); $myReturn = $body; } );

    I have confirmed that the length of the body ($archive) is the same as the size of the file on disk, so that's being loaded... I'm just not apparently getting any content back from the http_post - I'm not seeing the print statement in the anonymous call-back routine, which indicates I'm still not grasping the way the calls, and the call-backs, work.

    I think that the http_* methods work by making a call to the defined URL, and then process - upon completion of the http request sequence - the block that is the anonymous subroutine.... is that right?

    Does the original code have the requisite loop or holding stuff to wait until all the http calls have been made, and responded? (I think this may actually be my problem.)



    -- Ian Stuart
    A man depriving some poor village, somewhere, of a first-class idiot.

      If you use this code within your outer loop (the one with $cv->begin...$cv->end, then I think you're calling $cv->end too early and you never ->recv the guards, so I'm not sure that you ever enter the AnyEvent event loop...

      I think the intended usage of $cv->begin ...$cv->end is that the ->end call happens within the callback, not within the loop body:

      my $all_requests_done = AnyEvent->condvar; for my $r (@requests) { $all_requests_done->begin(); http_post $r, sub { ... $all_requests_done->end(); }; }; $all_requests_done->recv;

      Can you try to do a simple, self-contained program that does a single HTTP POST request and waits for it, without the ->begin ... ->end stuff?

      Update: (After two reboots due to power loss) added intended usage of ->begin ... ->end

      ... I'm just not apparently getting any content back from the http_post - I'm not seeing the print statement

      That doesn't sound right . I would use PERL_ANYEVENT_VERBOSE=1 perl yada... and maybe add

      use AnyEvent::Strict;
      and maybe add Devel::Trace (since AnyEvent::Trace is in the TODO que)

      I think that the http_* methods work by making a call to the defined URL, and then process - upon completion of the http request sequence - the block that is the anonymous subroutine.... is that right?

      Yes, http_* will invoke/call/execute the callback/nameless/anonymous-sub you pass, after its done what it can, either upon completion, or upon error -- the docs are very clear on this :)

      Does the original code have the requisite loop or holding stuff to wait until all the http calls have been made, and responded? (I think this may actually be my problem.)

      I think so. In fact, after reading some more documentation, I think with your original posted code, you could get rid of  my $cb = AnyEvent->condvar; and instead return

      return http_post( ...
      Then, instead of print Dumper(\@responses);
      for my $response ( @responses ){ print "$response ", $response->recv, "\n"; }

        Chee-nious!!!

        Yep - that (mostly) did it (I'm hanging, never ending - but that is a minor issue just now):

        The Dumper print is telling me I'm not getting my basic authentication through:

        { 'URL' => 'http://user:pass@host.name/my/path', 'connection' => 'close', 'Status' => '401', 'HTTPVersion' => '1.1', 'date' => 'Thu, 27 Jan 2011 16:05:54 GMT', 'content-length' => '0', 'Reason' => 'Authorization Required', 'content-type' => 'text/plain', 'www-authenticate' => 'Basic realm="SWORD"', 'x-error-code' => 'ErrorAuth', 'server' => 'Apache/2.2.13 (Unix) mod_perl/2.0.4 Perl/v5.1 +0.0' }

        .... however - I have just found http://kobesearch.cpan.org/htdocs/AnyEvent-Twitter-Stream/AnyEvent/Twitter/Stream.pm.html - which seems to have a solution to authenticated access to we sites & getting data..



        -- Ian Stuart
        A man depriving some poor village, somewhere, of a first-class idiot.