How to change rest service URL in PrintTask's json

653
3
07-31-2019 01:04 AM
MamoonRasheed
New Contributor

I have Print and Map Services on same physical server and web application (developed using ArcGIS Javascript API) also deployed on same server. Web application consumed both print and map services. 

When Print Task request send to server using LiveIP, at that moment Print Task crated json. This json has Map service with LiveIP. But i need localIP in Map service because Print Task request (using Live IP) send and now request is on server and its need to use Map service so Map service should have LocalIP in json.

var printTask = new PrintTask("http://<LIVEIP>:6080/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task"); 

To achieve this task i develop following code;

 

Step 1: alert(map.getLayer(map.graphicsLayerIds).url)  //Display LiveIP

Step2:

map.getLayer(map.graphicsLayerIds).url = http://<ServerLocalIP:port>/arcgis/rest/services/<serviceName>/MapServer/0          (Map object updated as per requirement)

 

Step3:

var params = new PrintParameters();

params.map = map;

 

Step4:

printTask.execute(params, function (evt) {

   // 

}

 

I checked in fiddler after send request ("printTask.execute"), the created json has LiveIP in Rest service URL instead of LocalIP.

 

Thanking you in advance.

0 Kudos
3 Replies
ReneRubalcava
Frequent Contributor

You might be able to achieve this with the Request Interceptor in 4x. You can modify the request for a given URL before it is sent.

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

Noah-Sager
Esri Regular Contributor

Something like this should work. You'll need to supply your own logic to swap out the MapService URL.

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: "https://utility.arcgisonline.com/arcgis/rest/services/Utilities/PrintingTools/GPServer/Export%20Web%20Map%20Task/execute",

  before: function(params) {

    const paramsURL = printService;

    const options = {
      query: {
        f: "json"
      },
      responseType: "json"
    };

    return esriRequest(paramsURL, options).then(function(response) {
    
      const old = old_webmap_as_json;
      const new = new_webmap_as_json;

      params.requestOptions.query.Web_Map_as_JSON = new;
    }
  }
});
MamoonRasheed
New Contributor

Its also possible in 3.X using esriRequest.setRequestPreCallback()

 

I solved my problem using following code;

 

Step 1:

 

on(dom.byId("btnPrint"), "click", function () {
         esriRequest.setRequestPreCallback(myCallbackFunction);

 

         printTask.execute(printObj.params, function (evt) {

                  document.getElementById("printResult").href = evt.url;

                  on(dom.byId("printResult"), "click", function () {
                           esriRequest.setRequestPreCallback(); // Detach myCallbackFunction()
          });
         }, function (evt) {

            esriRequest.setRequestPreCallback(); // Detach myCallbackFunction()

});  

}

 

Step 2:

function myCallbackFunction(args) {

         if (args.url.indexOf("execute") > -1) {
               var jsonObj = JSON.parse(args.content.Web_Map_as_JSON);
               var jsontxt = JSON.stringify(setLocalIP(jsonObj));
               args.content.Web_Map_as_JSON = jsontxt
         }
      return args;

}

 

Step 3:

 

function setLocalIP(strjson) {
      arrayUtils.forEach(strjson.operationalLayers, function (ol) {
      if (ol.id != "basemap") {
         if (ol.url != null) {   
            ol.url = LocalIP + ":" + ol.url.split(":")[2];   
      }
}
   }, this);

      return strjson;   
}

0 Kudos