Search code examples
xpagesxpages-ssjs

How to get the return value from a external rest service using SSJS


How to call external rest service using SSJS not using java code. I would like to call an external URL and then it returns a value, how to capture this value.


Solution

  • Do you want something a little like this:

    var url:java.net.URL = new java.net.URL("https://www.google.com");
    var conn:javax.net.ssl.HttpURLConnection = url.openConnection();
    
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Content-Type", "text/html");
    
    if(conn.getResponseCode() == 200) {
        var strReturn;
        var strOutput;
        var br:java.io.BufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream()));
        while((strOutput = br.readLine()) != null) {
            strReturn += strOutput;
        }
        conn.disconnect();
        return strReturn;
    
    } else {
        conn.disconnect();
        return "URL failure: " + conn.getResponseCode();
    }