Using withMiddleware()

  • Laravel's HTTP client provides methods to apply middleware using closures
  • Middleware ordering matters — they execute in the order they are added
  • Requests and responses follow the PSR-7 standard
use GuzzleHttp\Middleware;
use Illuminate\Support\Facades\Http;

Http::withMiddleware(Middleware::mapRequest(
    fn (RequestInterface $request) => ...
));

Http::withMiddleware(Middleware::mapResponse(
    fn (ResponseInterface $response) => ...
));

// Shortcut helpers that pass the closure to
// Middleware::mapRequest() and Middleware::mapResponse()
Http::withRequestMiddleware(
    fn (RequestInterface $request) => ...
);

Http::withResponseMiddleware(
    fn (ResponseInterface $response) => ...
);

← / → navigate · N notes · Esc exit