🀠
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
  • Default Query Parameters
  • Using the query Method
  • Available Methods
Edit on GitHub
  1. The Basics

Query Parameters

Like headers, query parameters can be added by using the query() method on either the connector or the request. When you add query parameters to a connector instance, every request sent through that connector will merge those parameters with the request. When you add query parameters to a request instance, it will just be added to that one request instance.

Default Query Parameters

You may configure default query parameters on the connector or request using the protected defaultQuery method.

<?php

use Saloon\Http\Connector;

class ForgeConnector extends Connector
{
    // {...}
    
    protected function defaultQuery(): array
    {
        return [
            'per_page' => 50,
        ];
    }
}

Default query parameters on a connector will be applied to every request that uses the connector.

<?php

use Saloon\Http\Request;

class GetServersRequest extends Request
{
    // {...}
    
    protected function defaultQuery(): array
    {
        return [
            'per_page' => 50,
        ];
    }
}

Using Properties With Default Query Parameters

You may also use properties in requests to populate query parameters, for example - requesting a specific page on a paginated API.

<?php

use Saloon\Http\Request;

class GetServersRequest extends Request
{
    // {...}
    
    protected int $perPage;
    
    protected int $page;
    
    public function __construct(int $perPage, int $page)
    {
        $this->perPage = $perPage;
        $this->page = $page;
    }
    
    protected function defaultQuery(): array
    {
        return [
            'per_page' => $this->perPage,
            'page' => $this->page,
        ];
    }
}
<?php

$request = new GetServersRequest(perPage: 50, page: 5);

Using the query Method

Saloon also offers a handy query parameters API to manage them easily after a request instance has been created. Use the query() method on your request to manage them. Query parameters added to the request are prioritised more than the connector's query parameters. This is useful for changing them before a request is sent.

<?php

$request = new GetServersRequest();

$request->query()->add('page', 5);

$all = $request->query()->all();

// array: ['page' => 5]

When you have default query parameters on a connector, they won't be visible to the request instance as they are merged later in the request lifecycle. Still, request query parameters will have a higher priority than connector query parameters.

Available Methods

set(array $items)

Overwrite the query parameters on the request with a new array.

merge(...$items)

Merge arrays of query parameters.

remove(string $key)

Remove a given query parameter by its key.

get(string $key, mixed $default = null)

Get a given query parameter by its key or return the default.

all()

Retrieve all query parameters as an array.

isEmpty

Check if the query parameter object is empty.

Click here to view the API reference for this method.

PreviousHeadersNextHTTP Client Config

Last updated 1 year ago

❓