How to authenticate to elastic search from java

I am trying to authenticate to ES in java. But getting malformed host header exception.
Someone please help here.

            final CredentialsProvider credentialsProvider =
                    new BasicCredentialsProvider();
                    new UsernamePasswordCredentials("", ""));

        RestClientBuilder builder = RestClient.builder(
                new HttpHost(elasticSearchHost, elasticSearchPort, "https"))
                .setDefaultHeaders(new Header[]{new BasicHeader("Authorization","ApiKey "+"")})
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder

        RestHighLevelClient client = new RestHighLevelClient(

Welcome. This is how I'm doing it:

Note that clusterUrl can be https://myinstance

1 Like

Great. It's working. Thanks

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.