Select to view content in your preferred language

Authenticate ArcGIS on premise

241
3
a month ago
OssamaHamed
New Contributor

I tried to do an auth. by using esriRequest.interceptors.push, but I couldn't get the response

here's my code:

require(["esri/Map",
"esri/views/MapView",
"esri/widgets/Search",
"esri/identity/IdentityManager",
"esri/identity/ServerInfo",
"esri/request",
"esri/config"], (Map, MapView, Search, IdentityManager, esriConfig, esriRequest) => {

if (esriRequest && esriRequest.interceptors) {
esriRequest.interceptors.push({
urls: "<on Premise ArcGIS url>", // Replace with your server URL pattern
before: function (params) {
console.log("Interceptor hit", params); // Log to check if the interceptor is working
params.requestOptions.headers = params.requestOptions.headers || {};
params.requestOptions.headers.Authorization = `Basic ${token}`;
},
error: function (error) {
console.error("Request error", error); // Log any errors in the request
return Promise.reject(error);
}
});
} else {
console.error("esriRequest or esriRequest.interceptors is not defined");
}

}

And always I get the "esriRequest or esriRequest.interceptors is not defined"

I use version 4.29 , anyone can solve this issue?

Tags (2)
0 Kudos
3 Replies
Sage_Wall
Esri Contributor

Hi @OssamaHamed,

esriRequest doesn't have a `interceptors` property.  This property needs to be set on esri config.

 https://developers.arcgis.com/javascript/latest/api-reference/esri-config.html#request

const featureLayerUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0";

esriConfig.request.interceptors.push({
  // set the `urls` property to the URL of the FeatureLayer so that this
  // interceptor only applies to requests made to the FeatureLayer URL
  urls: featureLayerUrl,
  // use the BeforeInterceptorCallback to check if the query of the
  // FeatureLayer has a maxAllowableOffset property set.
  // if so, then set the maxAllowableOffset to 0
  before: function(params) {
    if (params.requestOptions.query.maxAllowableOffset) {
      params.requestOptions.query.maxAllowableOffset = 0;
    }
  },
  // use the AfterInterceptorCallback to check if `ssl` is set to 'true'
  // on the response to the request, if it's set to 'false', change
  // the value to 'true' before returning the response
  after: function(response) {
    if (!response.ssl) {
      response.ssl = true;
    }
  }
});

 

0 Kudos
OssamaHamed
New Contributor

Hi @Sage_Wall ,

Thanks for your reply.

Actually I tried your approach with no luck.

The main target of my code to show the basemap in the web app by open a connection between the url of basemap through code.

esriConfig.request.interceptors.push({
urls: "<<Basemap Url>>", 
before: function (params) {
console.log("Interceptor hit", params); // Log to check if the interceptor is working
params.requestOptions.headers = params.requestOptions.headers || {};
params.requestOptions.headers.Authorization = `Basic ${token}`;
},
error: function (error) {
console.error("Request error", error); // Log any errors in the request
return Promise.reject(error);
}

0 Kudos
JohnGrayson
Esri Regular Contributor

Seems like you have esriRequest and esriConfig incorrectly configured, along with missing references. Try this instead:

require(["esri/Map",
"esri/views/MapView",
"esri/widgets/Search",
"esri/identity/IdentityManager",
"esri/identity/ServerInfo",
"esri/request",
"esri/config"], (EsriMap, MapView, Search, IdentityManager, ServerInfo, esriRequest, esriConfig) => {

 

0 Kudos