0

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.

Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76
user12039174
  • 49
  • 1
  • 5
  • 1
    What have you tried so far? Please enlighten us. And why no Java code? – Sven Hasselbach Sep 09 '19 at 08:15
  • My recommendation would be to a) use this as an opportunity to embrace Java. b) run it client-side from a JavaScript AJAX request in the browser instead c) if on 10.0.1, use a LotusScript agent and return the content to a temporary document, which your code uses Most people with experience of this will have gone down either the Java or JavaScript approach, and it provides experience for future development. The LotusScript approach is not ideal, but will give reusable experience for Notes Client development. – Paul Stephen Withers Sep 10 '19 at 09:43

1 Answers1

0

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();
}
  • Hi Andrew, thank you for your response. I am looking similar kind, but i want to read the returned json value. How can i do it. – user12039174 Sep 11 '19 at 02:49
  • @user12039174: What have you tried so far? Please enlighten us. – Sven Hasselbach Sep 11 '19 at 11:21
  • As Sven has said, it's easier for us to assist if you can post some code that you've tried. The best approach is definately doing this in Java, if you really want to work in SSJS then you'll need to add a SSJS library probably something like this: https://xomino.com/2013/12/18/safe-json-parsing-in-xpages-ssjs/ to convert the json string to an object you can work with. – Andrew Norrie Sep 12 '19 at 03:23
  • Andrew, it is obvious that @user12039174 did no research or coded something. The user just wanted YOU to do his job. For free. – Sven Hasselbach Sep 12 '19 at 05:01
  • Hi Sven, i was sick and so late response. If i know something about this ssjs i would have pasted my code but i am totally new to this one and so requested some examples. I always have a respect to the wise people who can address my questions and also double respect who criticize others. I am just changing the technology and so got into this issue. I am always thankful to Andrew that his code gave me a boost to get into ssjs as i was a python developer.Thanks – user12039174 Sep 18 '19 at 23:47
  • 1
    @user12039174: You were unable to do some reasearch before posting the question here? You can easily find a solution like this: https://stackoverflow.com/questions/21191273/consume-rest-service-in-xpage – Sven Hasselbach Sep 20 '19 at 05:52
  • I read the above solution but i dont want to go with complete java solution, the maintenance people requested us not to use java code – user12039174 Sep 22 '19 at 15:46