Admin Panel API: Request headers
Page summary:
app.unstable_addRequestHeaders()registers a provider whose headers are added to the requests the admin panel sends to the Strapi server. The headers go through the fetch client, merge between the defaults and the per-call headers, and must be allowed in the CORS configuration when the admin panel is served from another origin.
Before diving deeper into the concepts on this page, please ensure you have:
- created a Strapi plugin,
- read and understood the basics of the Admin Panel API
A plugin can add headers to the requests that the admin panel sends to the Strapi server, for example to tell the server which context the current browser tab works in. To do so, the plugin registers a provider: a function that returns the headers to add.
This API is unstable: it can change in a minor release.
- JavaScript
- TypeScript
export default {
register(app) {
// getCurrentContext() stands for the plugin's own logic
const removeHeaders = app.unstable_addRequestHeaders(() => ({
'x-my-plugin-context': getCurrentContext(),
}));
// Call removeHeaders() when the plugin no longer needs to add the headers
},
};
import type { StrapiApp } from '@strapi/admin/strapi-admin';
export default {
register(app: StrapiApp) {
// getCurrentContext() stands for the plugin's own logic
const removeHeaders = app.unstable_addRequestHeaders(() => ({
'x-my-plugin-context': getCurrentContext(),
}));
// Call removeHeaders() when the plugin no longer needs to add the headers
},
};
unstable_addRequestHeaders() returns a function that removes the provider.
unstable_addRequestHeaders(), unstable_getRequestHeaders(), and RequestHeadersError can also be imported from @strapi/strapi/admin. unstable_getRequestHeaders() returns the provider headers as a Headers object, for requests that don't go through the fetch client (see Affected requests).
CORS configuration
When the admin panel is served from a different origin than the server (see Deploy on different servers), the browser blocks the requests unless the header names are listed in the headers option of the strapi::cors middleware:
- JavaScript
- TypeScript
module.exports = [
// ...
{
name: 'strapi::cors',
config: {
headers: ['Content-Type', 'Authorization', 'Origin', 'Accept', 'x-my-plugin-context'],
},
},
// ...
];
export default [
// ...
{
name: 'strapi::cors',
config: {
headers: ['Content-Type', 'Authorization', 'Origin', 'Accept', 'x-my-plugin-context'],
},
},
// ...
];
This option replaces the default list instead of adding to it. Keep the default headers (Content-Type, Authorization, Origin, Accept) in the list.
Affected requests
The headers are added to:
- Every request made through the fetch client:
get(),post(),put(), anddel()ofgetFetchClient()anduseFetchClient()(see Fetch client). The Redux Toolkit Query (RTK Query)adminApiuses the fetch client in its base query, so its endpoints get the headers too. - Media Library uploads. They don't use the fetch client, so they call
unstable_getRequestHeaders()directly.
The headers are not added to:
- Login, logout, and other pre-login requests, for example
/admin/loginor/admin/register. These requests carry no context, so a failing provider can't block a login or a logout. - Requests sent with a plain
fetch()orXMLHttpRequest, including the plugin's own requests. To add the headers to such a request, callunstable_getRequestHeaders()and pass the returnedHeadersobject to the request.
Header merging
The fetch client starts from the default headers: Authorization, plus Accept and Content-Type, except for a get() with a responseType other than json. It then adds the provider headers, in the order the providers were registered, and finally the headers option of the call. A later source replaces an earlier one with the same name, and header names are case-insensitive.
A provider can't replace Authorization, Content-Type, or Accept. A value of undefined or null skips only that provider's entry for the header. The request then keeps the value from an earlier provider, or has no value for that header if none exists.
The provider is read once per call, before any retry. A retry after a 401 response therefore sends the same values as the first attempt, so a request can't switch context while the token refreshes.
Error handling
The request fails with a RequestHeadersError and is not sent when a provider:
- throws an error,
- returns something other than a plain object, for example a promise from an
asyncfunction, - or returns an invalid header name or value, for example a value that is not a string, is not Latin-1, or contains a line break.
The request is not sent because, without the header, the server could run it in the wrong context.
A forbidden header name, such as Cookie or Origin, does not cause an error: the browser drops the header silently.
isFetchError() returns false for a RequestHeadersError. With RTK Query endpoints, the error is serialized, so check its name property ('RequestHeadersError') instead of using instanceof.