0

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

3
  • 1
    Geoserver is expecting basic authentication, so you may need to set with credentials to true
    – Ian Turton
    Aug 21, 2019 at 11:31
  • your solution worked fine for 2) i'm able to get json output and read it in JS , but 1) i'm getting 500 Server Error i added the output of curl request from the cmd don't know if it can help, Thanks Aug 21, 2019 at 16:21
  • i ended using php's exec since it can run windows commands and it works just fine. Aug 23, 2019 at 0:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.