I am trying to programmatically upload a map into GeoServer as a layer. The user requirements as such that this data has to be uploaded from memory directly into GeoServer, using JavaScript. I looked up the REST API documentation and tried many things. I have posted the following posts one after another trying many approaches
Can anyone guide me in any approach in which I can upload to GeoServer using JavaScript REST API?
I am willing to use any data format, as long as it can be done through JavaScript REST API, but not Nodejs.
- Upload shapefile to GeoServer using JavaScript REST API (Previous error)
- Using Javascript to upload GeoJSON to Geoserver as a layer - posted posted after (and based on) this post
- Uploading GeoJSON as shapefile to GeoServer using REST API - posted posted after (and based on) this post
- Upload GeoJSON to GeoServer using CURL - posted posted after (and based on) this post
- Using Javascript to upload GeoJSON to Geoserver as a layer
Here is the latest code and error from GeoServer logs.
function UploadMap()
{
var shapeFile; //store the shapefile from url
console.log("Retrieving Shapefile to Load");
var httpRequest2 = new XMLHttpRequest(); // asynchronous request
httpRequest2.open("GET", "https://docs.mapbox.com/help/data/stations.zip", true);
httpRequest2.send();
httpRequest2.addEventListener("readystatechange", function() {
if (this.readyState === this.DONE) {
shapeFile = this.response;
console.log("shapeFile2 " + shapeFile2);
}
});
console.log('Loading Shapefile: ');
var xhr = new XMLHttpRequest();
console.log("XHR.Open");
xhr.open('PUT', 'https://DOMAIN/geoserver/rest/workspaces/ws/datastores/my_geonode_data/file.shp', true);
xhr.setRequestHeader('Content-Type', 'application/zip'); //;type=geojson
xhr.setRequestHeader("Authorization", "Basic " + btoa("USERNAME:PASSWORD"));
xhr.onload = function () {
console.log('responseText: ' + this.responseText);
};
console.log("Sending file for upload, XHR.Send ");
xhr.send(shapeFile);
var responseObject = xhr.responseText;
console.log('responseObject' + responseObject); // ------- Test status of upload
}
Error within Geoserver.log file
2022-02-03 02:03:55,568 ERROR [geoserver.rest] - Error occured unzipping file
org.geoserver.rest.RestException 500 INTERNAL_SERVER_ERROR: Error occured unzipping file
at org.geoserver.rest.catalog.AbstractStoreUploadController.handleFileUpload(AbstractStoreUploadController.java:132) at org.geoserver.rest.catalog.DataStoreFileController.doFileUpload(DataStoreFileController.java:577)
at org.geoserver.rest.catalog.DataStoreFileController.dataStorePut(DataStoreFileController.java:263)
at sun.reflect.GeneratedMethodAccessor511.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMeth
odAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
Caused by: java.util.zip.ZipException: zip file is empty
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:225)
at java.util.zip.ZipFile.<init>(ZipFile.java:155)
at java.util.zip.ZipFile.<init>(ZipFile.java:169)
at org.geoserver.rest.util.RESTUtils.unzipFile(RESTUtils.java:271)
at org.geoserver.rest.catalog.AbstractStoreUploadController.handleFileUpload(AbstractStoreUploadController.java:117)
caused by