on_headers
Summary
A callable that is invoked when the HTTP headers of the response have been received but the body has not yet begun to download.
Types
- callable
Constant
GuzzleHttp\RequestOptions::ON_HEADERS
The callable accepts a Psr\Http\ResponseInterface
object. If an exception is thrown by the callable, then the promise associated with the response will be rejected with a GuzzleHttp\Exception\RequestException
that wraps the exception that was thrown.
You may need to know what headers and status codes were received before data can be written to the sink.
// Reject responses that are greater than 1024 bytes.
$client->request('GET', 'http://httpbin.org/stream/1024', [
'on_headers' => function (ResponseInterface $response) {
if ($response->getHeaderLine('Content-Length') > 1024) {
throw new \Exception('The file is too big!');
}
}
]);
Note
When writing HTTP handlers, the on_headers
function must be invoked before writing data to the body of the response.