Learn how to use middleware to process HTTP requests and responses using the Instructor HTTP client API.
HttpMiddleware
interface directlyBaseMiddleware
abstract classHttpMiddleware
interface:
BaseMiddleware
abstract class is more convenient:
BaseMiddleware
, you only need to override the methods that matter for your middleware:
beforeRequest(HttpClientRequest $request): void
- Called before the request is sentafterRequest(HttpClientRequest $request, HttpResponse $response): HttpResponse
- Called after the response is receivedshouldDecorateResponse(HttpClientRequest $request, HttpResponse $response): bool
- Determines if the response should be decoratedtoResponse(HttpClientRequest $request, HttpResponse $response): HttpResponse
- Creates a decorated responseHttpResponse
interface. The library provides a BaseResponseDecorator
class that makes this easier: