cancel
Showing results for 
Search instead for 
Did you mean: 

Posting a data from XSJS into SAP Translation Hub API

former_member585658
Participant

Hello Colleagues,

I was experimenting SAP Translation HUB Api(https://saptranslation-s001*****9trial.hanatrial.ondemand.com/translationhub/api/v1/translate), Trace also not helping for xsjs.

i am getting empty array response. i have no idea where things go wrong.

Please help me here.

Initial tests with postman:

Authentication type : Basic

Content: Application/Json.

no csrf set and i get response succesfully.

Now in XSJS

i create destination file

description = "Translator";
host = "saptranslation-s001961**9trial.hanatrial.ondemand.com";
port = 80;
pathPrefix = "/translationhub/api/v1/";
//proxyType = none;
useProxy = true;
proxyHost = "proxy";
proxyPort = 8080;
authType = basic;
useSSL = false;
timeout = 3000;



In the XSADMIN i supplied the required username and password.

My XSJS Code

(Code 1)(Webrequest)

var dest = $.net.http.readDestination("translationPackage.xs.xsdestination", "translator");
var client = new $.net.http.Client();


//Make Request
var data = {
	"targetLanguages": [
    "fr"
  ],
	"sourceLanguage": "en",
	"enableMT": true,
	"enableTranslationQualityEstimation": true,
	"domain": "string",
	"companyMLTRId": "string",
	"units": [
		{
			"textType": "string",
			"key": "string",
			"value": "Love is great",
			"searchData": {
				"language": "string",
				"value": "string"
			}
    }
  ]
};
var jdata = JSON.stringify(data);


var req = new $.web.WebRequest($.net.http.POST, '/translate');


req.headers.set("Content-Type", "application/json");


req.setBody(jdata);


client.request(req, dest);


var response = client.getResponse();
$.response.contentType = "application/json";


$.response.status = $.net.http.OK;


$.response.setBody(JSON.stringify(response));

(Code 2)(http)

var data = {
	"targetLanguages": [
    "fr"
  ],
	"sourceLanguage": "en",
	"enableMT": true,
	"enableTranslationQualityEstimation": true,
	"domain": "string",
	"companyMLTRId": "string",
	"units": [
		{
			"textType": "string",
			"key": "string",
			"value": "Love is great",
			"searchData": {
				"language": "string",
				"value": "string"
			}
    }
  ]
};
var jdata = JSON.stringify(data);


var dest = $.net.http.readDestination("translationPackage.xs.xsdestination", "translator");
var client = new $.net.http.Client();


function doPOST() {


	try {


		var req = new $.net.http.Request($.net.http.POST, "translate");


		req.contentType = "application/json";


		req.setBody(jdata);


		client.request(req, dest);
		var response = client.getResponse();


		var res = JSON.stringify(response);


		$.response.contentType = "application/json";


		$.response.status = $.net.http.OK;


		$.response.setBody(JSON.stringify(res));


	} catch (err) {


		$.response.contentType = "text/plain";


		$.response.setBody("Error while executing query: [" + err.message + "]");


		$.response.returnCode = 200;


	}


}


doPOST();

Both of the code returns empty array { } as result.

Please tell me how to fix.

Thanks a lot.

Br,

Gabriel

former_member585658
Participant
0 Kudos

Do i need to recreate question ?Florian.

former_member585658
Participant
0 Kudos

Got it thanks 🙂

former_member635612
Discoverer
0 Kudos

Hi , I am also facing the same issue as its returning empty array as response.

Regards,

Shagil

former_member585658
Participant
0 Kudos

Hi, it seems for translation hub do-sent work in xsjs and it works only in postman. i noticed that in destinations when i placed a debugger ,even host is undefined ,url,port is undefined. thats why it returns empty response {}. more over we cannot create incident with SAP because this dosent fall under licensed version.(SAP Translation hub is not available in licensed production version. ) So we(Our team)( have decided to drop time and evaluation for this solution.

Hope it should be the answer Shagil.

Br,

Gabriel.

Accepted Solutions (0)

Answers (0)