Node: http://boardoftrustees-elk.kb.westeurope.azure.elastic-cloud.com:9243/, status: 400, [es/indices.create] Expecting JSON data but response content-type is: application/octet-stream

Hello guys. I have a project using spring boot and elastic cloud on azure.

When i run command mvn clean install i have an error like below.

Caused by: co.elastic.clients.transport.TransportException: node: http://trustboard-elk.kb.westeurope.azure.elastic-cloud.com:9243/, status: 400, [es/indices.create] Expecting JSON data but response content-type is: application/octet-stream
	at co.elastic.clients.transport.ElasticsearchTransportBase.checkJsonContentType(ElasticsearchTransportBase.java:471) ~[elasticsearch-java-8.13.2.jar:na]
	at co.elastic.clients.transport.ElasticsearchTransportBase.getApiResponse(ElasticsearchTransportBase.java:334) ~[elasticsearch-java-8.13.2.jar:na]
	at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:147) ~[elasticsearch-java-8.13.2.jar:na]
	at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:277) ~[elasticsearch-java-8.13.2.jar:na]
	at org.springframework.data.elasticsearch.client.elc.IndicesTemplate.lambda$doCreate$0(IndicesTemplate.java:145) ~[spring-data-elasticsearch-5.3.0.jar:5.3.0]
	at org.springframework.data.elasticsearch.client.elc.ChildTemplate.execute(ChildTemplate.java:71) ~[spring-data-elasticsearch-5.3.0.jar:5.3.0]
	... 65 common frames omitted
    @Override
    @NonNull
    public ClientConfiguration clientConfiguration() {

        return ClientConfiguration.builder()
                .connectedTo("trustboard-elk.kb.westeurope.azure.elastic-cloud.com:9243")
                .withHeaders(() -> {
                    HttpHeaders headers = new HttpHeaders();
                    headers.add("Authorization", "ApiKey *myApiKey*");
                    return headers;
                })
                .build();
    }
}

my config bean is above.

How can i fix this.?

You are connecting the Elasticsearch client to Kibana instead of connecting to Elasticsearch I think.

Yess. Sorry.

    @Override
    @NonNull
    public ClientConfiguration clientConfiguration() {

        return ClientConfiguration.builder()
                .connectedTo("https://a43468016c62e64.westeurope.azure.elastic-cloud.com:92432")
                .withHeaders(() -> {
                    HttpHeaders headers = new HttpHeaders();
                    headers.add("Authorization", "ApiKey **MyApiKey**");
                    return headers;
                })
               .build();
    }

}

This is true config. But now,

Caused by: java.net.UnknownHostException: https://a43468016c62e64.westeurope.azure.elastic-cloud.com:443: nodename nor servname provided, or not known
        at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
        at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:52)
        at java.base/java.net.InetAddress$PlatformResolver.lookupByName(InetAddress.java:1211)
        at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1828)
        at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:1139)
        at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1818)
        at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1688)
        at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:666)
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager$InternalAddressResolver.resolveRemoteAddress(PoolingNHttpClientConnectionManager.java:637)
        at org.apache.http.nio.pool.AbstractNIOConnPool.processPendingRequest(AbstractNIOConnPool.java:474)
        at org.apache.http.nio.pool.AbstractNIOConnPool.lease(AbstractNIOConnPool.java:280)
        at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.requestConnection(PoolingNHttpClientConnectionManager.java:295)
        at org.apache.http.impl.nio.client.AbstractClientExchangeHandler.requestConnection(AbstractClientExchangeHandler.java:381)
        at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.start(DefaultClientExchangeHandlerImpl.java:130)
        at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.execute(CloseableHttpAsyncClientBase.java:116)
        at org.apache.http.impl.nio.client.InternalHttpAsyncClient.execute(InternalHttpAsyncClient.java:138)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:300)
        ... 92 more

And wnen i try like this without https://

    @Override
    @NonNull
    public ClientConfiguration clientConfiguration() {

        return ClientConfiguration.builder()
                .connectedTo("a43708ceefac456cb859468016c62e64.westeurope.azure.elastic-cloud.com:9243")
                .withHeaders(() -> {
                    HttpHeaders headers = new HttpHeaders();
                    headers.add("Authorization", "ApiKey o0LY2pgXR0KSqQt2duSeTA");
                    return headers;
                })
                .build();
    }
}

Error is same

Caused by: co.elastic.clients.transport.TransportException: node: http://a43708ceefac456cb859468016c62e64.westeurope.azure.elastic-cloud.com:9243/, status: 400, [es/indices.create] Expecting JSON data but response content-type is: application/octet-stream
        at co.elastic.clients.transport.ElasticsearchTransportBase.checkJsonContentType(ElasticsearchTransportBase.java:471)
        at co.elastic.clients.transport.ElasticsearchTransportBase.getApiResponse(ElasticsearchTransportBase.java:334)
        at co.elastic.clients.transport.ElasticsearchTransportBase.performRequest(ElasticsearchTransportBase.java:147)
        at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:277)
        at org.springframework.data.elasticsearch.client.elc.IndicesTemplate.lambda$doCreate$0(IndicesTemplate.java:145)
        at org.springframework.data.elasticsearch.client.elc.ChildTemplate.execute(ChildTemplate.java:71)
        ... 86 more

Go to your cloud console and just copy the Elasticsearch endpoint url. Paste it in your code.

The logs you shared and the code are sometimes inconsistent so I'm not sure about what you're doing exactly.

I still run on debug mode.

There is a decompiled class called ElasticsearchTransportBase.class

And my related method is below

    private void checkJsonContentType(
        String contentType, TransportHttpClient.Response clientResp, Endpoint<?, ?, ?> endpoint
    ) throws IOException {
        if (contentType == null) {
            throw new TransportException(clientResp, "Response has no content-type", endpoint.id());
        }

        if (contentType.startsWith("application/json") || contentType.startsWith("application/vnd.elasticsearch+json")) {
            return;
        }

        throw new TransportException(clientResp, "Expecting JSON data but response content-type is: " + contentType, endpoint.id());
    }


If you inspect this image, ClinetResp object give me error ....

Becouse contentType is come application/octet-stream but expected is application-json

I think it comes with the wrong content type because you are not hitting the right endpoint. I can see in the capture that your are using http and not https.

I am using withouth https. At the same time, i am not use http. Just a43708ceefac456cb859468016c62e64.westeurope.azure.elastic-cloud.com:9243

And when i connect to with https://a43708ceefac456cb859468016c62e64.westeurope.azure.elastic-cloud.com:9243 code give me nodename nor servname provided or not known error.