Skip to content

Accept header lost if transport initialized with LLRC RequestOptions #377

Closed
@swallez

Description

@swallez

Creating a RestClientTransport with a LLRC RequestOptions leads every request to fail with
co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/indices.create] failed: [media_type_header_exception] Invalid media-type value on headers [Accept, Content-Type]

How to reproduce

transport = new RestClientTransport(restClient, mapper)
    .withRequestOptions(new RestClientOptions.Builder(
        RequestOptions.DEFAULT.toBuilder()
        // add other options as needed
    ).build());
client = new ElasticsearchClient(transport);

Explanation

The Java API client sends a Content-Type: "application/vnd.elasticsearch+json;compatible-with=8" header with every request, and the ES server expects to see the same value for the Accept header. Setting custom RequestOptions override the Java API client built-in RequestOptions that sets this header.

Workaround

Manually set the Accept header:

transport = new RestClientTransport(restClient, mapper)
    .withRequestOptions(new RestClientOptions.Builder(
        RequestOptions.DEFAULT.toBuilder()
            .addHeader("Accept", "application/vnd.elasticsearch+json;compatible-with=8")
            // add other options as needed
    ).build());
client = new ElasticsearchClient(transport);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions