Search code examples
javaspring-bootelasticsearch

Elasticsearch 8.13 headers


I am trying to connect to Elasticsearch 8.13.0 server using Spring Boot 3.3.3, but on startup my Sping boot service throws an error:

co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/indices.create] failed: [media_type_header_exception] Invalid media-type value on headers [Content-Type, Accept]

I'm creating RestClient in a following way:

@Bean
public RestClient restClient() {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(elasticCredentials.getUsername(), elasticCredentials.getPassword()));

    return RestClient.builder(new HttpHost(elasticProperties.getHost(), elasticProperties.getPort()))
            .setHttpClientConfigCallback(httpClientBuilder -> {
                httpClientBuilder.disableAuthCaching();
                httpClientBuilder.setDefaultHeaders(List.of(
                        new BasicHeader(
                                HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
                ));
                httpClientBuilder.addInterceptorLast((HttpResponseInterceptor)
                        (response, context) ->
                                response.addHeader("X-Elastic-Product", "Elasticsearch"));
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }).build();
}

I've already tried setting Accept header to "application/json" or "application/vnd.elasticsearch+json;compatible-with=8"? like this:

@Bean
public RestClient restClient() {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(elasticCredentials.getUsername(), elasticCredentials.getPassword()));

    return RestClient.builder(new HttpHost(elasticProperties.getHost(), elasticProperties.getPort()))
            .setHttpClientConfigCallback(httpClientBuilder -> {
                httpClientBuilder.disableAuthCaching();
                httpClientBuilder.setDefaultHeaders(List.of(
                        new BasicHeader(
                                HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()),
                        new BasicHeader(
                                HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString())
                ));
                httpClientBuilder.addInterceptorLast((HttpResponseInterceptor)
                        (response, context) ->
                                response.addHeader("X-Elastic-Product", "Elasticsearch"));
                return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            }).build();
}

And the only thing that has changed was an error message:

Invalid media-type value on headers [Accept]

...the same error but without Content-Type.

Can anyone help me with this problem?


Solution

  • The problem surprisingly resolved after I removed ContentType header from RestClient. Hope this helps anyone who will struggle with this.