[GIS] Uploading shapefile to geoserver using cURL

curlgeoserverpython

I'm building the curl command in a python script, but when I run the script I get this error:

* Connected to localhost (127.0.0.1) port 8081
* Server auth using Basic with user 'admin'
> PUT /geoserver/rest/workspaces/csEvo/datastores/ABSM/file.shp HTTP/1.1
> Authorization: Basic YWRtaW46Y3NlbWVyZ2Vuc3lz
> User-Agent: curl/7.15.1 (i386-pc-win32) libcurl/7.15.1 OpenSSL/1.0.1c zlib/1.2.3
> Host: localhost:8081
> Accept: */*
> Content-type: application/zip
> Content-Length: 16302
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
HTTP/1.1 500 Internal Server Error
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Server: Jetty(6.1.8)
Error occured unzipping file:error in opening zip file* Connection #0 to host localhost left intact
* Closing connection #0

but if I run the same built curl command directly in the cmd line it works.

My py script is as follows (zips the shapefile and uploads it into geoserver) and I'm running it from inside the OSGEO4W shell in order to inherit environment settings:

#!/usr/bin/env python
import os
import zipfile
from subprocess import call
# Geoserver Variables
gshost = "http://localhost:8081"
gsuser = "admin"
gspassw = "geoserver"
gsworkspace = "csEvo"
gsstore = "teste"
AB_shpfile = "C:/data/theshpfile.shp"
#zip file for Geoserver upload
thezipfile = zipfile.ZipFile(AB_shpfile.replace('.','').replace('shp', '.zip'), "w")
for filenametozip in [ABSM_shpfile, ABSM_shpfile.replace('.shp', '.dbf'), AB_shpfile.replace('.shp', '.shx'), ABSM_shpfile.replace('.shp', '.prj')]: 
       arcname=os.path.basename(filenametozip.replace('.',''))
       arcname=str(arcname).replace(str(arcname)[-3:], '.' + str(arcname)[-3:])
       thezipfile.write(filenametozip, arcname)
# Send to Geoserver
thefileforgs = AB_shpfile.replace('.','').replace('shp', '.zip')
call("curl -u "+gsuser+":"+gspassw+" -v -XPUT -H " + '"Content-type: application/zip"' + " --data-binary @"+'"'+str(thefileforgs)+'"' + ' ' + gshost +"/geoserver/rest/workspaces/"+gsworkspace+"/datastores/"+gsstore+"/file.shp", shell=True)

What am I missing?

Best Answer

If you want to do it with Python, you're better off using gsconfig as opposed to system calls using the subprocess module. It's a Python library for manipulating a GeoServer instance via the GeoServer RESTConfig API. You can check the documentation for more information.

Related Question