To optimize my Perl application I need to work with async HTTP requests, so I can handle other operations once the HTTP response is finish. So I believe my only option is to work with HTTP::Async module. This works fine for simple requests, but I need to catch cookie header from one response and send it with next one, so I need to read headers. My code is:
...
$async->add($request);
while ($response = $async->wait_for_next_response)
{
threads->yield(); yield();
}
$cookie = $response->header('Set-Cookie');
$cookie =~ s/;.*$//;
$request->header('Cookie' => $cookie);
...
but it’s not working, as it ends with an error Can’t call method “header” on an undefined value. Obviously $response is undef. How can I catch headers before $response gets undef?
Is guaranteed not to finish until
$responseis false. The only false valuewait_for_next_responsewill return isundef. You need to either extract the cookie inside the loop, or cache the last good response inside the loop.Something like
should work, although I’m not sure you need the loop at all. It’s hard to tell without a complete program.