How to specify Authorization Header for a source in mapbox-gl-js?

前端 未结 1 757
醉话见心
醉话见心 2021-02-08 12:41

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-encod         


        
相关标签:
1条回答
  • 2021-02-08 13:13

    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 optionally headers and credentials 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 }
          }
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题