Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save krisrice/9f8d85bbdb195dfe248db0caa1f7447c to your computer and use it in GitHub Desktop.
Save krisrice/9f8d85bbdb195dfe248db0caa1f7447c to your computer and use it in GitHub Desktop.
script
//
// AS A SQLcl SCRIPT
//
var json = {"INPUT": "hello world"};
var reply = httpPost("HTTP://API.SHOUTCLOUD.IO/V1/SHOUT", JSON.stringify(json));
ctx.write("\n\n");
ctx.write(JSON.stringify(reply));
ctx.write("\n\n");
/*************
UTILITY FUNCTIONS
*************/
function httpGet(theUrl){
var con = new java.net.URL(theUrl).openConnection();
con.requestMethod = "GET";
return asResponse(con);
}
function httpPost(theUrl, data, contentType){
contentType = contentType || "application/json";
var con = new java.net.URL(theUrl).openConnection();
con.requestMethod = "POST";
con.setRequestProperty("Content-Type", contentType);
// Send post request
con.doOutput=true;
write(con.outputStream, data);
return asResponse(con);
}
function asResponse(con){
var d = read(con.inputStream);
return {data : d, statusCode : con.responseCode};
}
function write(outputStream, data){
var wr = new java.io.DataOutputStream(outputStream);
wr.writeBytes(data);
wr.flush();
wr.close();
}
function read(inputStream){
var inReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
var inputLine;
var response = new java.lang.StringBuffer();
while ((inputLine = inReader.readLine()) != null) {
response.append(inputLine);
}
inReader.close();
return response.toString();
}
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment