βοΈRequests
API requests in Saloon each have a class. Within a request class, you define the endpoint, the HTTP method, and properties like headers, query parameters and body.
Getting Started
Create a class that is in a similar place to your connector. The class should extend the Request
abstract class. Inside the class, define a $method
property and a resolveEndpoint
method. The resolveEndpoint
method will be combined with the connector's base URL.
If you have installed the Laravel plugin, you can use the php artisan saloon:request command to create a request.
Headers
Some API requests require headers to be sent. To define default headers on your request, you can extend the defaultHeaders
method. These will be merged with the connector's headers.
You can also use the headers
method on a request instance.
Query Parameters
Some API requests require query parameters to be sent. You can extend the defaultQuery
method to provide these query parameters.
You can also use the query
method on a request instance.
Different APIs sometimes require different query string formats. Saloon uses PHP's built-in http_build_query
function to create query strings, but if you need something different, you can reformat the query string in the Connector::handlePsrRequest()
method (see Modifying the PSR-7 Request.
Timeout
By default, Saloon will have a connection timeout of 10 seconds and a request timeout of 30 seconds. You can customise this by using the HasTimeout
trait and specifying a connectTimeout
and requestTimeout
property.
Constructor Arguments
Since requests are just classes, you can define a constructor to populate the request's properties. For example, if you are getting a specific resource you would need to pass its ID into the request class.
Last updated