Thursday, 2 July 2015

geoserver - curl REST API to AJAX


I'm trying to make a web application that is able to :


1) Upload raster data to GeoServer so when I use this command on cmd it works


curl -u admin:geoserver -v -XPUT -H "Content-type: image/tiff" --data-binary @"C:\Path\odm_orthophoto.tif"  http://localhost:8080/geoserver/rest/workspaces/raster_data/coveragestores/int_dec/file.geotiff

but on AJAX it doesn't work, giving me error 401 unauthorized here's my code


$.ajax({    
beforeSend: function (xhr) {

xhr.setRequestHeader ("Authorization", "Basic YWRtaW46Z2Vvc2VydmVy");
},
url: "http://localhost:8080/geoserver/rest/workspaces/raster_data/coveragestores/int_decc/file.geotiff",
contentType: "image/tiff",
method : 'PUT',
xhrFields:{
withCredentials: true
},
data : '--data-binary @"C:/Path/odm_orthophoto.tif"' ,
success : function(data){

console.log(data);
},
error : function(httpReq,status,exception){
alert(status+" "+exception);
}
});

I also tried to use


 beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic' + 'admin:geoserver'

}

but still getting the same error. I enabled CORS in web.xml in WEB-INF GeoServer folder, without the jetty.jar file like said here https://gis.stackexchange.com/questions/210109/enabling-cors-in-geoserver-jetty because adding it give me error 503 when opening geoserver.


2) Read layer's name from the coverage store also with REST API it works on Windows with this command


curl -u admin:geoserver -XGET http://localhost:8080/geoserver/rest/workspaces/raster_data/coveragestores/int_dec/coverages.json


My JavaScript code is :


$.ajax({
url: "http://localhost:8080/geoserver/rest/workspaces/raster_data/coveragestores/int_dec/coverages.json",
type: 'GET',
xhrFields: {

withCredentials: false
},
dataType: 'jsonp',
success : function(data){
console.log(data);
},
error : function(httpReq,status,exception){
alert(status+" "+exception);
}
});


but I get this error



unexpected token: ':'



When I add


xhrFields: {
withCredentials: false
}


to AJAX parameters I get the same error as 1) 401 unauthorized , which should eliminate CORS problem according to https://stackoverflow.com/questions/38953965/uncaught-syntaxerror-and-cors-error-using-ajax-with-jsonp/38956911#38956911 using json as datatype returns the same error


I'm using GeoServer 2.15 and JRE 8u221 32bits


UPDATE :


using withCredentials: true , worked for getting json list but not for the PUT method i get know HTTP/1.1 500 Server Error here's the output of my curl command i think i need to change url to http:/localhost:8080 but i don't know what to do with the /geoserver/rest/workspaces/raster_data/coveragestores/int_decc/file.geotiff enter image description here




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...