Skip to content

Latest commit

 

History

History
298 lines (134 loc) · 4.97 KB

Middleware.md

File metadata and controls

298 lines (134 loc) · 4.97 KB

Middleware

Functions used to create and wrap handlers with handler middleware.

  • Full name: \GuzzleHttp\Middleware
  • This class is marked as final and can't be subclassed
  • This class is a Final class

Methods

cookies

Middleware that adds cookies to requests.

public static cookies(): callable

The options array must be set to a CookieJarInterface in order to use cookies. This is typically handled for you by a client.

  • This method is static.

Return Value:

Returns a function that accepts the next handler.


httpErrors

Middleware that throws exceptions for 4xx or 5xx responses when the "http_errors" request option is set to true.

public static httpErrors(\GuzzleHttp\BodySummarizerInterface|null $bodySummarizer = null): callable
  • This method is static.

Parameters:

Parameter Type Description
$bodySummarizer \GuzzleHttp\BodySummarizerInterface|null The body summarizer to use in exception messages.

history

Middleware that pushes history data to an ArrayAccess container.

public static history(array|\ArrayAccess<int,array>& $container): callable
  • This method is static.

Parameters:

Parameter Type Description
$container array|\ArrayAccess<int,array> Container to hold the history (by reference).

tap

Middleware that invokes a callback before and after sending a request.

public static tap(callable $before = null, callable $after = null): callable

The provided listener cannot modify or alter the response. It simply "taps" into the chain to be notified before returning the promise. The before listener accepts a request and options array, and the after listener accepts a request, options array, and response promise.

  • This method is static.

Parameters:

Parameter Type Description
$before callable Function to invoke before forwarding the request.
$after callable Function invoked after forwarding.

Return Value:

Returns a function that accepts the next handler.


redirect

Middleware that handles request redirects.

public static redirect(): callable
  • This method is static.

Return Value:

Returns a function that accepts the next handler.


retry

Middleware that retries requests based on the boolean result of invoking the provided "decider" function.

public static retry(callable $decider, callable $delay = null): callable

If no delay function is provided, a simple implementation of exponential backoff will be utilized.

  • This method is static.

Parameters:

Parameter Type Description
$decider callable Function that accepts the number of retries,
a request, [response], and [exception] and
returns true if the request is to be retried.
$delay callable Function that accepts the number of retries and
returns the number of milliseconds to delay.

Return Value:

Returns a function that accepts the next handler.


log

Middleware that logs requests, responses, and errors using a message formatter.

public static log(\Psr\Log\LoggerInterface $logger, \GuzzleHttp\MessageFormatterInterface|\GuzzleHttp\MessageFormatter $formatter, string $logLevel = &#039;info&#039;): callable
  • This method is static.

Parameters:

Parameter Type Description
$logger \Psr\Log\LoggerInterface Logs messages.
$formatter \GuzzleHttp\MessageFormatterInterface|\GuzzleHttp\MessageFormatter Formatter used to create message strings.
$logLevel string Level at which to log requests.

Return Value:

Returns a function that accepts the next handler.


prepareBody

This middleware adds a default content-type if possible, a default content-length or transfer-encoding header, and the expect header.

public static prepareBody(): callable
  • This method is static.

mapRequest

Middleware that applies a map function to the request before passing to the next handler.

public static mapRequest(callable $fn): callable
  • This method is static.

Parameters:

Parameter Type Description
$fn callable Function that accepts a RequestInterface and returns
a RequestInterface.

mapResponse

Middleware that applies a map function to the resolved promise's response.

public static mapResponse(callable $fn): callable
  • This method is static.

Parameters:

Parameter Type Description
$fn callable Function that accepts a ResponseInterface and
returns a ResponseInterface.