Last active
August 29, 2015 14:16
-
-
Save geobabbler/48d450b80f2966cb5716 to your computer and use it in GitHub Desktop.
Posting a new PostGIS feature type to GeoServer using the REST config API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//callback should be like function(err){//do stuff} | |
function registervector(datasetName, callback){ | |
//dataset name is the name of the PostGIS table | |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; //my dev SSL is self-signed. don't do this in production. | |
var https = require('https'); //using SSL because of basic auth in this example | |
var auth = 'Basic ' + new Buffer('username' + ':' + 'p@s$w0rd').toString('base64'); //strong passwords please | |
//build the object to post | |
var post_data = {'featureType': {'name': datasetName}}; | |
//be sure to turn it into a string | |
var s = JSON.stringify(post_data); | |
var post_options = { | |
host: 'my.geoserver.io', | |
port: '443', | |
path: '/rest/workspaces/myworkspace/datastores/mypgdatastore/featuretypes', | |
method: 'POST', | |
headers: { | |
'Content-Length': s.length, | |
'Content-Type': 'application/json', | |
'Authorization': auth | |
} | |
} | |
// Set up the request | |
var post_req = https.request(post_options, function(res) { | |
res.setEncoding('utf8'); | |
//201 is good, anything else is RIP | |
if (res.statusCode === 201){ | |
res.on('data', function (chunk) { | |
}); | |
//since we're good, call back with no error | |
callback(null); | |
} | |
else{ | |
res.on('data', function (chunk) { | |
//something went wrong so call back with error message | |
callback(chunk); | |
}); | |
} | |
}); | |
// post the data | |
post_req.write(s); | |
post_req.end(); | |
}; | |
//here's how you call it | |
registervector('mytable', function(err){ | |
if(err){ | |
//RIP | |
console.log(err); | |
} | |
else{ | |
//your table will appear as a new layer in GeoServer admin UI | |
console.log('success'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment