Built-in Middleware

  • Guzzle ships with middleware that runs on all requests when using the default handler stack
  • httpErrors — throws exceptions for 4xx and 5xx responses
  • Redirect — adds tracking details to response headers
  • Cookies — attaches cookies to the request when a cookie jar is present
  • PrepareBody — adds Content-Length, Content-Type, and Expect headers when the request has a body
public static function create(callable $handler = null): self
{
    $stack = new self($handler ?: Utils::chooseHandler());
    $stack->push(Middleware::httpErrors(), 'http_errors');
    $stack->push(Middleware::redirect(), 'allow_redirects');
    $stack->push(Middleware::cookies(), 'cookies');
    $stack->push(Middleware::prepareBody(), 'prepare_body');

    return $stack;
}

← / → navigate · N notes · Esc exit