HTTP Post Scripts
From Observer GigaFlow Support | VIAVI Solutions Inc.
var utils = Java.type('ros.CROSUtils'); var json = {action:"get",type: "subnets"}; var response = httpsPost("https://172.21.21.12:8902/rest/?userName=admin&userPass=admin", JSON.stringify(json),"application/json").data; log.warn(response); var respo = JSON.parse(response); log.warn(respo); 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 httpsPost(theUrl, data, contentType){ contentType = contentType || "application/json"; var X509TrustManager = Java.type("javax.net.ssl.X509TrustManager"); var permissiveTrustManager = Java.extend(X509TrustManager, { getAcceptedIssuers: function(){return null;}, checkClientTrusted: function(certs, authType){return;}, checkServerTrusted: function(certs, authType){return;} } ); var trustAllCerts = [new permissiveTrustManager()]; // Install the all-trusting trust manager var sc = javax.net.ssl.SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // Create all-trusting host name verifier var HostnameVerifier = Java.type("javax.net.ssl.HostnameVerifier"); var allHostsValid = Java.extend(HostnameVerifier,{ verify: function(hostname,session){return true;}}); // Install the all-trusting host verifier javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new allHostsValid() ); 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(); }