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.

  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 invoked before writing data to the body of the response.