Esri Leaflet GP Service as Post request

4644
4
Jump to solution
11-04-2015 01:50 AM
Labels (1)
FC_Basson
MVP Regular Contributor

I'm trying to send an Export Web Map request to my AGS print service with the Esri Leaflet library (ver 2.0.0 beta), but the GET request string is too long.  Is it possible to run a geoprocessing task as a POST request without using a proxy page?

0 Kudos
1 Solution

Accepted Solutions
JohnGravois
Frequent Contributor

unfortunately this is kind of a 'you cant have your cake and eat it too' situation because you can't POST across domains when the server you're trying to reach doesn't support CORS.

you have two options.

1. if the print server actually supports CORS, you can accept the default constructor option and the plugin will automatically switch to a POST when the request is longer than 2000 characters

{ useCors: true }

2. if either the print server (or client browser) doesn't support CORS, you'll need to fall back on proxying the request to accomodate the security restriction.

see the conceptual article here for more information.

View solution in original post

0 Kudos
4 Replies
FC_Basson
MVP Regular Contributor

In the meantime I have just recreated the print request with an Ajax call, which makes it possible to perform the GP task as a POST request.  Would still like to know how to use the Esri Leaflet library with POST type GP requests.

jQuery Ajax

$.ajax({
   url:'http://myserver/arcgis/rest/services/GP_Services/ExportWebMap/GPServer/Export%20Web%20Map/execute',
   type:'POST',
   dataType:'json',
   data:({
      Format:"JPG",
      Layout_Template:"A4 Landscape",
      Web_Map_as_JSON:JSON.stringify(mapJSON),
      f:'json'
   }),
   success:function(response){
      console.log(response.results[0].value.url);
   },
   error:function(xhr, status, error) {
      console.log(error);
   }
})

vs

Esri Leaflet GP task

var printService = L.esri.GP.service({
   url: "http://myserver/arcgis/rest/services/GP_Services/ExportWebMap/GPServer/Export%20Web%20Map",
   async:false,   
   useCors:false
});
var printTask = printService.createTask();
printTask.setParam("Format", "JPG");
printTask.setParam("Layout_Template", "A4 Landscape");
printTask.setParam("f","json");
printTask.setParam("Web_Map_as_JSON",JSON.stringify(mapJSON));
printTask.setOutputParam('Output_File');
printTask.run(function(error, response, raw) {            
   var printout = response.Output_File.url;
   console.log(printout);
});
0 Kudos
JohnGravois
Frequent Contributor

unfortunately this is kind of a 'you cant have your cake and eat it too' situation because you can't POST across domains when the server you're trying to reach doesn't support CORS.

you have two options.

1. if the print server actually supports CORS, you can accept the default constructor option and the plugin will automatically switch to a POST when the request is longer than 2000 characters

{ useCors: true }

2. if either the print server (or client browser) doesn't support CORS, you'll need to fall back on proxying the request to accomodate the security restriction.

see the conceptual article here for more information.

0 Kudos
FC_Basson
MVP Regular Contributor

Thanks John Gravois, luckily the print server I'm using supports CORS and setting the useCors parameter to true worked. 

0 Kudos
JohnGravois
Frequent Contributor

no problem at all.

i figured the server was up to the challenge, otherwise your jQuery call would have bombed out too

0 Kudos