Esri-arcgis request intercept based on wildcard route

633
1
12-02-2020 04:24 AM
yzimmermann
New Contributor

I'm trying to implement a MapLayer in my react application. The map is only accessible over a proxy which needs authentication to identify the users for each end every request.

To provide the token i added the following request interceptor to make sure all calls which access that backend would be expanded by the authorization. If i do it this way there is no header added to the requests.

 

 

 

esriConfig.request.interceptors.push({
  urls: ['backend/api/*'],
  before: async function (params: any) {
    params.requestOptions.headers = {
      Authorization: 'Bearer ' + token),
    };
  },
});

 

 

 

 

If i remove the urls parameter so that all routes are extended with the token I'll recieve some cors errors because the esri package also makes some calls to another api where i get blocked because i'm sending an authorization header which is not expected.

Other api: https://www.arcgis.com/sharing/rest/portals/self?f=json&culture=de-de

How can i make sure only requests to my api will be extended with the autorization header?

0 Kudos
1 Reply
ReneRubalcava
Frequent Contributor II

Can you try changing it to a regex, like this

esriConfig.request.interceptors.push({
  urls: [/\/backend\/api\//],
  ...
});

 I don't think you even need the wildcard, but if you used the full domain, like "https://myserver/backend/api" it should get recognized. We don't parse for wildcards, but will check if the request url starts with a string or test a regex you provide.