Cleaning Null Values Safely in Laravel Collections
When a Laravel collection holds optional values, a later transformation may reject null. Remove nulls before that step if it expects a…
When Laravel code uses the HTTP client to call an external API, the client builds and sends a request. Request middleware runs before the client sends the request. It can inspect the request or return a changed version. You can attach middleware to one request while you configure it, or register it centrally for calls made through Laravel’s HTTP client.
Use request middleware when you need to change what gets sent, such as adding a shared header. Use Laravel’s HTTP-client events to inspect requests for logging or monitoring. Request middleware returns the request Laravel sends to the server. The documented RequestSending event gives a listener a request to inspect.
Call withRequestMiddleware to attach a callback to one request while you configure it. Laravel passes the callback a PSR-7 request, an HTTP message that follows a set of PHP interfaces. Methods such as withHeader return a new request instance. The callback must return that new instance for Laravel to send it. The Laravel HTTP Client documentation shows how to return the modified request.
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Psr\Http\Message\RequestInterface;
$response = Http::withRequestMiddleware(
function (RequestInterface $request) {
Log::debug('Sending outbound request', [
'method' => $request->getMethod(),
'host' => $request->getUri()->getHost(),
]);
return $request->withHeader('X-Application', 'Example');
}
)->get('https://api.example.test/resource');
The callback records the request’s method and host, then returns a request with an added header. You can use the same pattern to inspect other request details or change the request before Laravel sends it. Avoid logging authorization headers, credentials, or sensitive request bodies.
Laravel’s HTTP client uses Guzzle, a PHP library for sending HTTP requests. Guzzle middleware wraps the handler that sends the request; each layer receives the request and passes it to the next. Lower-level Guzzle middleware can also work with the response as the call returns. See Guzzle’s explanation of handlers and middleware.
Register globalRequestMiddleware to apply the same change to requests made through Laravel’s HTTP client. Laravel documents registering it in a service provider’s boot method:
use Illuminate\Support\Facades\Http;
public function boot(): void
{
Http::globalRequestMiddleware(
fn ($request) => $request->withHeader(
'User-Agent',
'Example Application/1.0'
)
);
}
This registration applies to requests sent through Laravel’s HTTP client. It does not intercept browser requests to your Laravel site or calls made through unrelated HTTP clients. Use request-scoped middleware when only one outgoing call needs the change; use global middleware for a rule shared by Laravel HTTP-client calls.
Laravel’s RequestSending event fires before a request is sent and carries a request that a listener can inspect. Use the event to monitor or log outgoing requests. The event lets a listener inspect the request; use request middleware when you need to return a changed request. Laravel also documents ResponseReceived and ConnectionFailed events for inspecting later stages of a call.
An event listener can log request details without changing the request. To make a change, such as adding a header, use withRequestMiddleware or globalRequestMiddleware. The callback returns the request Laravel should send.
Laravel’s current HTTP-client documentation describes withRequestMiddleware and globalRequestMiddleware. If you work with an older codebase, consult Laravel’s versioned 11.x documentation. Check the documentation for your installed Laravel version before using either method.
Laravel’s documentation also says you cannot chain pool with middleware or other HTTP-client methods such as withHeaders. Set options on each request inside the pool instead. For more on Laravel HTTP pools, see the Laravel HTTP pools overview.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com