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.

  1. // Reject responses that are greater than 1024 bytes.
  2. $client->request('GET', 'http://httpbin.org/stream/1024', [
  3. 'on_headers' => function (ResponseInterface $response) {
  4. if ($response->getHeaderLine('Content-Length') > 1024) {
  5. throw new \Exception('The file is too big!');
  6. }
  7. }
  8. ]);

Note

When writing HTTP handlers, the on_headers function must be invokedbefore writing data to the body of the response.