on_headers
- Summary
A callable that is invoked when the HTTP headers of the response havebeen 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 exceptionis thrown by the callable, then the promise associated with the response willbe rejected with a GuzzleHttp\Exception\RequestException
that wraps theexception that was thrown.
You may need to know what headers and status codes were received before datacan 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 invokedbefore writing data to the body of the response.