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

Headers

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

Default Headers

You may configure default headers on the connector or request using the protected defaultHeaders method.

<?php

use Saloon\Http\Connector;

class ForgeConnector extends Connector
{
    // {...}
    
    protected function defaultHeaders(): array
    {
        return [
            'Content-Type' => 'application/json'
        ];
    }
}

Default headers on a connector will be applied to every request. This is handy for providing specific headers like Content-Type or Accept.

<?php

use Saloon\Http\Request;

class GetServersRequest extends Request
{
    // {...}
    
    protected function defaultHeaders(): array
    {
        return [
            'Content-Type' => 'application/json'
        ];
    }
}

Using Properties With Default Headers

You may also use properties in requests to populate headers, for example populating a username custom header.

<?php

use Saloon\Http\Request;

class GetServersRequest extends Request
{
    // {...}
    
    protected string $username;
    
    public function __construct(string $username)
    {
        $this->username = $username;
    }
    
    protected function defaultHeaders(): array
    {
        return [
            'X-Username' => $this->username,
        ];
    }
}
<?php

$request = new GetServersRequest('Sammyjo20');

// X-Username: Sammyjo20

Using the Headers Method

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

<?php

$request = new GetServersRequest();

$request->headers()->add('Content-Type', 'application/json');

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

// Content-Type: application/json

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

Available Methods

set(array $items)

Overwrite the headers on the request with a new array.

merge(...$items)

Merge arrays of headers.

remove(string $key)

Remove a given header by its key.

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

Get a given header by its key or return the default.

all()

Retrieve all headers as an array.

isEmpty

Check if the header object is empty.

Click here to view the API reference for this method.

PreviousRequestsNextQuery Parameters

Last updated 1 year ago

πŸ€“