๐Ÿšช
Saloon
๐Ÿง‘โ€๐Ÿ’ป View Code On Github๐Ÿค  Version 3 is out!๐Ÿ’– Support The Project
v1 (Security Fixes Only)
v1 (Security Fixes Only)
  • ๐ŸšชSaloon
  • ๐Ÿš€Getting Started
    • Installation
  • ๐Ÿค The Basics
    • Connectors
    • Requests
    • Attaching Data
    • Query Parameters
    • Sending Requests
    • Responses
      • Data Transfer Objects
    • Building SDKs
  • ๐Ÿ‘Next Steps
    • Authentication
    • Plugins
    • Caching
    • Mocking Responses
      • Non-Laravel / PHP
      • Laravel
      • Recording Requests
  • ๐ŸงชAdvanced
    • OAuth2 Authentication
    • Response Interceptors
    • Handlers / Middleware
  • โญFinish
    • Showcase
    • Tutorials
    • View Code On GitHub
    • Update Docs
    • About Me
    • Donate
Powered by GitBook
On this page
Edit on GitHub
  1. Advanced

Handlers / Middleware

Last updated 2 years ago

If you need to modify the underlying Guzzle request/response right before it is sent, you can use handlers. This is an incredibly useful feature that Guzzle provides to view/modify the request before it is sent.

To add a handler or middleware, simply use the addHandler method in your plugin or boot method on your connector/request.

Example

use Psr\Http\Message\RequestInterface;

class CreateForgeServerRequest extends SaloonRequest
{
    //...

    public function boot(SaloonRequest $request): void
    {
        $this->addHandler('customHeaderHandler', function (callable $handler) {
            return function (RequestInterface $request, array $options) use ($handler) {
                $request->withHeader('X-Custom-Header', 'Hello');
                
                return $handler($request, $options);             
            };
        });
    }
}

Saloon will not know about any extra headers, configuration or data you add inside of handlers.

๐Ÿงช
Click here to read more about Guzzle Handlers / Middleware