πŸ“‘Responses

After sending a request, Saloon will return a Response class. This response class contains many helpful methods for interacting with your HTTP response like seeing the HTTP status code and retrieving the body.

<?php

$forge = new ForgeConnector;
$request = new GetServersRequest;

$response = $forge->send($request);

$status = $response->status();
$body = $response->body();

By default, Saloon will not throw an exception if a synchronous request fails. Refer to the handling failures section for handling errors.

Useful Methods

Below are some of the key methods provided by Saloon's Response class.

Asynchronous Responses

When sendAsync or concurrent requests, Saloon will respond with a GuzzleHttp\Promise\PromiseInterface. The promise will contain a Response a class described above. When the request fails, Saloon will not use the then method but return an instance of RequestExceptionin the otherwise block.

<?php

use Saloon\Http\Response;

$forge = new ForgeConnector;
$promise = $forge->sendAsync(new GetServersRequest);

$promise
    ->then(function (Response $response) {
        // Handle successful response
    })
    ->otherwise(function (Exception $exception) {
        // Handle failed request
    });

Custom Responses

Sometimes you may want to use your response class. This is useful if you want to add your methods or overwrite Saloon's response methods. Saloon allows you to overwrite the response at a connector level for all requests or at a per-request level for a granular response.

The simplest way of registering a custom response is to use the $response property on either the connector or request.

class ForgeConnector extends Connector
{
    // ...
    
    protected ?string $response = CustomResponse::class;
}

When you need a more advanced way to define a custom response, use the resolveResponseClass method on either the connector or request.

class ForgeConnector extends Connector
{
    // ...
    
    public function resolveResponseClass(): string
    {
        return CustomResponse::class;
    }
}

Last updated