Forked from billybong/nashorn javascript http client
Last active
February 16, 2017 20:22
-
-
Save krisrice/9f8d85bbdb195dfe248db0caa1f7447c to your computer and use it in GitHub Desktop.
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
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