How do I set a request header for a wms source with mapbox-gl-js? I need all tile requests to add a header that looks like:
Authorization: "Bearer base64-encoded-token"
The WMS example, map#addSource and map#addLayer lead me to believe it is not possible to set tile request headers.
To send a request with the Bearer Token authorization header, you need to make an HTTP request and provide your Bearer Token with the "Authorization: Bearer {token}" header. A Bearer Token is a cryptic string typically generated by the server in response to a login request.
Add a vector source to a map. Add any Mapbox-hosted tileset using its tileset URL ( mapbox:// + tileset ID). To find a list of Default tilesets provided by Mapbox and Custom tilesets you have uploaded to your account, sign into Mapbox Studio and visit the Tilesets page.
The HTTP Authorization request header can be used to provide credentials that authenticate a user agent with a server, allowing access to a protected resource. The Authorization header is usually, but not always, sent after the user agent first attempts to request a protected resource without credentials.
You can now use the transformRequest
option to add a custom header:
A callback run before the Map makes a request for an external URL. The callback can be used to modify the url, set headers, or set the credentials property for cross-origin requests. Expected to return an object with a
url
property and optionallyheaders
andcredentials
properties.
Example:
const map = new mapboxgl.Map({
container: 'map',
center: [2.35, 48.86],
zoom: 13,
transformRequest: (url, resourceType) => {
if (resourceType === 'Source' && url.startsWith('http://myHost')) {
return {
url: url,
headers: { 'Authorization': 'Bearer ' + yourAuthToken }
}
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With