0

i need to help to create json request from xpages to yandex Json interface api. How to configure json https request for this api. https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/#codes

or

How to use this block in xpages

 URL myURL = new URL(serviceURL);
 HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
 String userCredentials = "username:password";
 String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
 myURLConnection.setRequestProperty ("Authorization", basicAuth);
 myURLConnection.setRequestMethod("POST");
 myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
 myURLConnection.setRequestProperty("Content-Language", "en-US");
 myURLConnection.setUseCaches(false);
 myURLConnection.setDoInput(true);
 myURLConnection.setDoOutput(true); 

sample code blok like this

https://translate.yandex.net/api/v1.5/tr.json/translate ? 
key=<API key>
& text=<text to translate>
& lang=<translation direction>
& [format=<text format>]
& [options=<translation options>]
& [callback=<name of the callback function>]
  • It looks like you got a couple good answers going on. I would add my own, but it wouldn't be a terribly distinguishing answer and the prepared example I have at the moment [from my blog](https://edm00se.io/xpages/rest-consumption-server-side/) would need a little updating, in my opinion. I would probably take Stephan's (stwissel) approach as it would be easy to implement in either an `xe:restService` like he mentions, or others. – Eric McCormick Dec 11 '16 at 21:09

2 Answers2

3

Easiest way to do this is to wrap it in a simple Java class with a parameter free constructor (a.k.a a Bean) and then call it from server side JavaScript like:

var myYandex = new myYandexBean();
myYandex.callServer(somethingtoTranslate);

For callback I would recommend to use a rest control, so it would look like /yournsf.nsf/somepage.xsp/specialName

stwissel
  • 20,110
  • 6
  • 54
  • 101
1

In addition to Stephan's answer you could create your own custom REST service with a structure like this example: https://stash.openntf.org/projects/DOMEX/repos/dominoexplorer/browse/ODP/Code/Java/net/notesx/domex/rest/AllDatabasesService.java

Oliver Busse
  • 3,375
  • 1
  • 16
  • 26