Translate File SDL Language Cloud REST API

Hi!

I'm unable to use https://lc-api.sdl.com/file-translations API in a Java environment.

This is my source:

HttpEntity entity = MultipartEntityBuilder
.create()
.addTextBody("from", "ita")
.addTextBody("to", "eng")
.addBinaryBody("file", file, ContentType.create("application/octet-stream"), filename)
.build();

HttpPost httpPost = new HttpPost(urlStr);
httpPost.setEntity(entity);

httpPost.addHeader("content-type", "multipart/form-data");
httpPost.addHeader("authorization", "LC apiKey=myKey");
httpPost.addHeader("termbaseIds", "[myTermbase]");

HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = null;

try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
if (response != null){
return EntityUtils.toString(response.getEntity());
}
else{
return null;
}

 

When run the code, server reply:

{"message":"An error occured while processing your request!","errorCode":"GENERIC_APPLICATION_EXCEPTION","details":[]}

Wrong something?

 

UPDATE 18-6-17:

The getStatusLine() method return 500 Internal Server Error.

 

Thanks for the reply!

Alessandro

Parents
  • Hello!
    The problem is that you are trying to upload the file as binary body. You should try to add it to the body as a multipart form data.

    Here is the code:

    HttpEntity entity = MultipartEntityBuilder
    .create()
    .addTextBody("from", "ita")
    .addTextBody("to", "eng")
    .addPart("file", new FileBody(file, ContentType.MULTIPART_FORM_DATA, "filename"))
    .build();

    HttpPost httpPost = new HttpPost("lc-api.sdl.com/file-translations");
    httpPost.setEntity(entity);
    httpPost.addHeader("authorization", "LC apiKey=myapikey");
    httpPost.addHeader("termbaseIds", "[myTermbase]");

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse response = null;

    try {
    String responseString;
    response = httpClient.execute(httpPost);
    responseString = EntityUtils.toString(response.getEntity());
    System.out.println(responseString);
    } catch (IOException e) {
    e.printStackTrace();
    }

    Hope it helps.
Reply
  • Hello!
    The problem is that you are trying to upload the file as binary body. You should try to add it to the body as a multipart form data.

    Here is the code:

    HttpEntity entity = MultipartEntityBuilder
    .create()
    .addTextBody("from", "ita")
    .addTextBody("to", "eng")
    .addPart("file", new FileBody(file, ContentType.MULTIPART_FORM_DATA, "filename"))
    .build();

    HttpPost httpPost = new HttpPost("lc-api.sdl.com/file-translations");
    httpPost.setEntity(entity);
    httpPost.addHeader("authorization", "LC apiKey=myapikey");
    httpPost.addHeader("termbaseIds", "[myTermbase]");

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse response = null;

    try {
    String responseString;
    response = httpClient.execute(httpPost);
    responseString = EntityUtils.toString(response.getEntity());
    System.out.println(responseString);
    } catch (IOException e) {
    e.printStackTrace();
    }

    Hope it helps.
Children