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

but how DO I get the response from each PUT into my list of responses?

Replace  return $cb; with something else, which is the thing you want :)(i can't tell what you want, or how to get it, but that is where)

Replies are listed 'Best First'.
Re^2: AnyEvent::HTTP and getting data back from a post request
by Anonymous Monk on Jan 27, 2011 at 13:15 UTC
    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
      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"; }
Re^2: AnyEvent::HTTP and getting data back from a post request
by kiz (Monk) on Jan 27, 2011 at 15:02 UTC

    :chuckle:

    My bad - I should have stated that: each request returns either a standard http error code or a piece of XML (Atom, to be more precise)



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