Anil
Anil

Reputation: 45

POSTing to cross-domain REST service from within an XPage

What is the best way to call a cross-domain REST service from within an XPage, I've looked through the Social Enabler app, but in this case I need to also POST a string of content to this service on another server, and then get the response which is JSON and parse it.

Upvotes: 1

Views: 1336

Answers (4)

Niklas Heidloff
Niklas Heidloff

Reputation: 962

Option 1: JSONP Example: http://openntf.org/XSnippets.nsf/snippet.xsp?id=xsnippets-widget Code: http://xsnippets.openntf.org

Option 2: Domino/iNotes proxy http://www.openntf.org/Projects/pmt.nsf/DA2F4D351A9F15B28625792D002D1F18/%24file/SocialEnabler111006.pdf section 05.01

Option 3: Your own generic proxy as plugin http://www.openntf.org/Projects/pmt.nsf/DA2F4D351A9F15B28625792D002D1F18/%24file/SocialEnabler111006.pdf section 05.02

Option 4: As described above. Implement server side code to access other servers.

Upvotes: 4

jjtbsomhorst
jjtbsomhorst

Reputation: 1667

There are several options:

  1. Create an xpage which generates json. This is called xagent ( more info)
  2. Create a bean at application scope wich contains code to retrieve data from te service directly using the java url connection and parses the json in a vector or any other java object you can work with
  3. Do the same as 2 except constructan instance of the url connection directly in the event code.

For both option 2 and option 3 you could use code like this

URL url = new URL(http://yourresthot/restapi);
HttpCOnnection conn = url.openConnection();


if (conn.getResponseCode() != 200) {
    throw new IOException(conn.getResponseMessage());
  }

  // Buffer the result into a string
  BufferedReader rd = new BufferedReader(
      new InputStreamReader(conn.getInputStream()));
  StringBuilder sb = new StringBuilder();
  String line;
  while ((line = rd.readLine()) != null) {
    sb.append(line);
  }
  rd.close();

  conn.disconnect();
  return sb.toString();

Upvotes: 2

Thomas Adrian
Thomas Adrian

Reputation: 3636

You can use java URLConnection to fetch the json data using ssjs and by using "eval" or "toJSON" you can put it in a repeat and display the result with a computed field.

Upvotes: 0

Serdar Basegmez
Serdar Basegmez

Reputation: 3355

Cross-domain REST service is not possible from the client side Javascript.

There is a workaround for that, using JSONP if the remote service supports this. It basically consists of a script tag referring to another javascript resource on the remote site which is allowed by browsers.

Other than that, if you use a server side REST proxy, you may call the remote service from your XPages. This method is being used for iNotes for instance to integrate with Sametime or Quickr.

http://www-10.lotus.com/ldd/lqwiki.nsf/dx/iNotes_proxy_configuration_qd85

This page illustrates how to configure the embedded proxy for your service.

Upvotes: 5

Related Questions