Skip to content

How to customize OkHttpClient #903

@694551594

Description

@694551594

Description
Currently, the Config class in the Meilisearch Java SDK initializes a default HttpClient as a final field, with no way to inject a custom OkHttpClient instance. This prevents users from configuring advanced HTTP settings such as custom timeouts, connection pooling, interceptors (e.g., logging, retry logic), or proxy configurations — which are essential for production-grade applications.

Basic example
I would like to be able to pass a pre-configured OkHttpClient to the Config class, like this:

OkHttpClient customClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.addInterceptor(new LoggingInterceptor())
.retryOnConnectionFailure(true)
.build();

Config config = new Config("http://localhost:7700", "masterKey", customClient);
MeiliSearchClient client = new MeiliSearchClient(config);
Currently, this is not possible because Config only accepts hostUrl and apiKey, and internally creates a fixed HttpClient. Adding a constructor that accepts an OkHttpClient (or HttpClient interface) would enable full control over HTTP behavior.

Other

This change would improve flexibility for enterprise users who need custom HTTP behavior (e.g., TLS config, corporate proxies, monitoring).
The change is backward-compatible — existing code continues to work with the default constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions