# Getting ML job with Java API on Elastic Cloud

**URL:** <https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-machine-learning\
**Created:** [August 2, 2018, 4:36pm UTC](https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790 "2018-08-02T16:36:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbailey](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dbailey](https://discuss.elastic.co/u/dbailey)\
**Post date:** [August 2, 2018, 4:36pm UTC](https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790/1 "2018-08-02T16:36:15Z")

</div>

Hi there, I noticed that running a simple GET \_xpack/ml/anomaly\_detectors/job-that-does-not-exist through the Java low-level REST client against a trial Elastic Cloud deployment returns a 400 BAD\_REQUEST instead of a 404. Have tested this against my local ES cluster and also on the cloud trial through the Dev Tools and the API console and it correctly returns a 404 with "resource\_not\_found\_exception". I suspect none of the ML API calls are working but can do further testing to verify.

Is this some restriction pertaining to the Java API or maybe because it is a trial deployment? ES is 6.3.2 on both clusters.

Thanks.

---

<div class="post-metadata">

**Author:** ![dkyle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dkyle/32/59114_2.png) [@dkyle](https://discuss.elastic.co/u/dkyle)\
**Post date:** [August 3, 2018, 12:27pm UTC](https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790/2 "2018-08-03T12:27:57Z")

</div>

Hi,

I have not been able to recreate this with the low-level client or using curl. You should use the URL of the Elasticsearch API endpoint, is it possible you are accessing the cluster via a different URL? I believe cloud use a proxy and I wonder if you are hitting this rather than directly to the cluster

---

<div class="post-metadata">

**Author:** ![dbailey](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dbailey](https://discuss.elastic.co/u/dbailey)\
**Post date:** [August 3, 2018, 1:54pm UTC](https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790/3 "2018-08-03T13:54:37Z")

</div>

Hello, I have tried with curl and it does work, this returns a 200 as expected:

`curl -i -u user:pass -X GET https://8a0570efd6b54daf955f87b8e17c8cf7.eu-central-1.aws.cloud.es.io:9243/_xpack/ml/anomaly_detectors`

but using the same endpoint and credentials through Java I still get the 400 bad request error. Here's the code:

```
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass"));
        
restClientBuilder = RestClient.builder(HttpHost.create("https://8a0570efd6b54daf955f87b8e17c8cf7.eu-central-1.aws.cloud.es.io:9243")).setHttpClientConfigCallback(new HttpClientConfigCallback() {
    @Override
    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        	return httpClientBuilder;
        }
    });
        
client = new RestHighLevelClient(restClientBuilder);
        
try {
    Response response = client.getLowLevelClient().performRequest("GET", "_xpack/ml/anomaly_detectors");
    System.out.println(response);
    System.out.println(response.getEntity());
} catch (Exception e) {
    e.printStackTrace();
}
```

---

<div class="post-metadata">

**Author:** ![dbailey](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dbailey](https://discuss.elastic.co/u/dbailey)\
**Post date:** [August 3, 2018, 2:27pm UTC](https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790/4 "2018-08-03T14:27:01Z")

</div>

OK I got it, I was actually not able to do any requests using the low-level client, I was missing a forward slash at the beginning of the URI:

client.getLowLevelClient().performRequest("GET", " **/** \_xpack/ml/anomaly\_detectors");

It used to work in 6.2.2 without the slash so must be a change in the 6.3.2 library. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![dkyle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dkyle/32/59114_2.png) [@dkyle](https://discuss.elastic.co/u/dkyle)\
**Post date:** [August 3, 2018, 2:40pm UTC](https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790/5 "2018-08-03T14:40:03Z")

</div>

Great news, thanks for the update

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [October 29, 2018, 10:04pm UTC](https://discuss.elastic.co/t/getting-ml-job-with-java-api-on-elastic-cloud/142790/6 "2018-10-29T22:04:22Z")

</div>


