🀠
Saloon
GithubSupport Project
v2
v2
  • 🀠Saloon
  • Upgrade
    • 🎁What's new in v2
    • 🌿Upgrading from v1
  • Getting Started
    • πŸ‘‹Installation
  • The Basics
    • πŸ”ŒConnectors
    • βœ‰οΈRequests
    • πŸ€“Headers
    • ❓Query Parameters
    • πŸ”§HTTP Client Config
    • πŸ“¦Sending Body/Data
      • JSON Body
      • Multipart Form Body
      • Form Body (URL Encoded)
      • XML Body
      • String / Plain Text Body
      • Stream Body
    • πŸ”Authentication
    • πŸš€Sending Requests
    • πŸ“‘Responses
    • πŸ›€οΈData Transfer Objects
    • πŸ”₯Handling Failures
    • πŸͺBuilding SDKs
  • Digging Deeper
    • ☝️Solo Requests
    • πŸ“–Pagination v1 (Old)
    • πŸ“–Pagination v2
      • Paged Pagination
      • Limit/Offset Pagination
      • Cursor Pagination
      • Custom Pagination
    • 🎯Retrying Requests
    • 🏎️Concurrency & Pools
    • πŸ”‘OAuth2 Authentication
      • Authorization Code Grant
      • Client Credentials Grant
    • πŸ’‚Middleware
    • ⏸️Request Delay
    • πŸ›©οΈSenders
    • πŸ‘ͺPlugins
  • Plugins
    • β›΅Laravel Helpers
    • πŸ”Caching Responses
    • β›”Handling Rate Limits
    • 🏭SDK Generator
  • Testing
    • πŸ“ΈRecording Responses
    • 🚧Mock Responses
  • Conclusion
    • 🍳Cookbook
    • πŸ“šTutorials & Blog Posts
    • 🐞Known Issues
    • πŸ€—Credits
    • πŸŽ–οΈShowcase
    • ❀️Support
    • πŸ‘‹About Me
Powered by GitBook
On this page
  • Getting Started
  • Asynchronous Requests
  • Sending Solo Requests
  • Sending requests without instantiating the connector
  • Request Properties
Edit on GitHub
  1. The Basics

Sending Requests

PreviousAuthenticationNextResponses

Last updated 1 year ago

Getting Started

To start sending requests, instantiate your connector class and request class and use the send or sendAsync methods. When using the send method. you will receive a Response class.

<?php

$forge = new ForgeConnector('api-token');
$request = new GetServersRequest;

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

Asynchronous Requests

Saloon supports asynchronous requests out of the box. Use the sendAsync method, and you will receive an instance of PromiseInterface. Saloon uses Guzzle's Promises library, which uses the A+ standard. .

<?php

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

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

Sending Solo Requests

<?php

$request = new GetServersRequest;
$response = $request->send();

You have the following methods available on the solo request.

  • send(MockClient $mockClient = null)

  • sendAsync(MockClient $mockClient = null)

  • createPendingRequest

  • connector

Sending requests without instantiating the connector

With previous versions of Saloon, you could send a request directly without having to use a connector to send a request. While sending requests through the connector has many benefits, you may wish to add this feature with the HasConnector trait on your request.

Adding the trait

Once you have added the trait to your request, make sure to add the connector protected property and define your connector class. You may instead extend the resolveConnector method if you need a more advanced solution.

<?php

use Saloon\Http\Request;
use Saloon\Traits\Request\HasConnector;
use App\Http\Integrations\LaravelForge\Forge;

class GetServersRequest extends Request
{
    use HasConnector;
    
    protected string $connector = Forge::class;

    protected string $method = 'GET';

    public function resolveEndpoint(): string
    {
        return '/servers';
    }
}

When defining a connector with a property, you must not have any constructor properties on your connector.

<?php

use Saloon\Http\Request;
use Saloon\Traits\Request\HasConnector;
use App\Http\Integrations\LaravelForge\Forge;

class GetServersRequest extends Request
{
    use HasConnector;

    protected string $method = 'GET';
    
    protected function resolveConnector(): Connector
    {
        return new ForgeConnector;
    }

    public function resolveEndpoint(): string
    {
        return '/servers';
    }
}

Sending requests

<?php

$request = new GetServersRequest;
$response = $request->send();

Now you can use the following methods on your request.

  • send(MockClient $mockClient = null)

  • sendAsync(MockClient $mockClient = null)

  • createPendingRequest

  • connector

Request Properties

You may also overwrite any headers, query parameters, HTTP client config and request body on the connector or request. Read through the sections above for all the methods on the request property methods.

<?php

$forge = new ForgeConnector('api-token');

// All requests sent will have the header and query parameter applied

$forge->headers()->add('X-Custom-Header', 'Hello'!);
$forge->query()->add('page', 5);
<?php

$forge = new ForgeConnector('api-token');
$request = new GetServersRequest;

// The single request will have the additional header and query parameter.

$request->headers()->add('X-Custom-Header', 'Hello'!);
$request->query()->add('page', 5);

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

Saloon supports all the features Guzzle offers for asynchronous requests, including unwrapping promises and request pooling for high-performance API calls.

Please make sure to read the section on first to configure your request. You can send solo requests directly.

When sending multiple requests for the same service, use the same connector instance as it has a over using a new connector instance for every request.

πŸš€
Click here to read more
Click here to learn more.
solo requests
significant performance improvement