Skip to content

[DOCS] Rest 5 client fixes #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Basic authentication

Configuring basic authentication can be done by providing an `HttpClientConfigCallback` while building the `RestClient` through its builder. The interface has one method that receives an instance of [`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`](https://hc.apache.org/httpcomponents-asyncclient-4.1.x/current/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html) as an argument and has the same return type. The http client builder can be modified and then returned. In the following example we set a default credentials provider that requires basic authentication.
To use basic authentication in the REST 5 client, set a default authorization header:

% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-basic-auth
```java
Expand All @@ -16,7 +16,9 @@ Rest5ClientBuilder restClient = Rest5Client

```

Preemptive Authentication can be disabled, which means that every request will be sent without authorization headers to see if it is accepted and, upon receiving an HTTP 401 response, it will resend the exact same request with the basic authentication header. If you wish to do this, then you can do so by disabling it via the `HttpClientConfigCallback`:
To configure authentication behavior, pass an `HttpClientConfigCallback` when building the `Rest5Client`. The callback's single method takes an instance of [`org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder`](https://hc.apache.org/httpcomponents-client-5.5.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.html) as an argument and returns the same type, so you can modify the provided builder and return it for the client to use.

By default, the HTTP client uses preemptive authentication: it includes credentials in the initial request. You might want to use non-preemptive authentication, which sends a request without credentials and retries with the header after a `401 Unauthorized` challenge. To do this, set an `HttpClientConfigCallback` with auth caching disabled:

% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-disable-preemptive-auth
```java
Expand All @@ -26,12 +28,12 @@ var creds = Base64.getEncoder().encodeToString("user:test-user-password".getByte

Rest5ClientBuilder restClient = Rest5Client
.builder(new HttpHost("https", "localhost", 9200))
.setHttpClientConfigCallback(HttpAsyncClientBuilder::disableAuthCaching)
.setHttpClientConfigCallback(HttpAsyncClientBuilder::disableAuthCaching) <1>
.setDefaultHeaders(new Header[]{
new BasicHeader("Authorization", "Basic " + creds)
});
```

1. Disable preemptive authentication

1. Disables preemptive authentication

For more options, refer to [](other_authentication_methods.md).
30 changes: 19 additions & 11 deletions docs/reference/transport/rest5-client/config/timeouts.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@

# Timeouts

Configuring requests timeouts can be done by using the `setRequestConfigCallback` method while building the `RestClient`. In the following example we increase the connect timeout (defaults to 30 second) and the response timeout (defaults to 0, which is infinite).
You can set timeouts when building the `Rest5Client`:

- The **connect timeout** is the maximum time for establishing a TCP connection, including the TLS handshake. The connect timeout is set on `ConnectionConfig`.
- The **socket timeout** is the maximum time to wait for I/O on an established socket. The socket timeout is set on `ConnectionConfig`.
- The **response timeout** is the maximum period to wait for response data. The response timeout is set on `RequestConfig`.
- The **connection request timeout** is the maximum time for leasing a connection from the pool. The connection request timeout is set on `RequestConfig`.

To configure timeouts, use `setConnectionConfigCallback` and `setRequestConfigCallback` while building the `Rest5Client`. The following example sets a 10-second connect timeout, a 10-second socket timeout, and a 20-second response timeout:

% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-timeouts
```java
Rest5ClientBuilder builder = Rest5Client
.builder(new HttpHost("localhost", 9200))
.builder(new HttpHost("http", "localhost", 9200)) <1>
.setConnectionConfigCallback(connectConf -> connectConf
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor arguments are in the wrong order. According to the HttpHost constructor, the correct order should be new HttpHost("localhost", 9200, "http") where hostname comes first, then port, then scheme.

Suggested change
.builder(new HttpHost("http", "localhost", 9200)) <1>
.builder(new HttpHost("localhost", 9200, "http")) <1>

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@l-trotta I'm not sure Copilot is right about this. On this apache page, it says "For constructors that take a scheme as an argument, that argument is now the first one."

Should http be first or last?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah my bad for mixing it up in the example, copilot is still wrong though ^^" correct order is: scheme, host, port. so it should be .builder(new HttpHost("http","localhost", 9200))

Copy link
Contributor Author

@marciw marciw Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should http be first or last?

I went ahead and put it first, but happy to revert

.setConnectTimeout(Timeout.ofSeconds(10))
.setSocketTimeout(Timeout.ofSeconds(10)))
.setRequestConfigCallback(r -> r
.setConnectTimeout(Timeout.of(5000, TimeUnit.MILLISECONDS))
.setResponseTimeout(Timeout.of(30000, TimeUnit.MILLISECONDS))
.build()
.setResponseTimeout(Timeout.ofSeconds(20))
);
```

Timeouts also can be set per request with RequestOptions, which overrides RestClient's builder. The RequestOptions can then be set in the Rest5ClientTransport constructor.
1. Specify `https` for TLS.

You can also set per-request timeouts using `RequestOptions`, which override the builder defaults. The following example sets a response timeout of 60 seconds, as well as a connection request timeout of 1 second (to limit pooled connection wait time):

% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-request-options-timeouts
```java
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(Timeout.ofMilliseconds(5000))
.setConnectionRequestTimeout(Timeout.ofMilliseconds(60000))
RequestConfig requestConfig = RequestConfig.custom()
.setResponseTimeout(Timeout.ofSeconds(60))
.setConnectionRequestTimeout(Timeout.ofSeconds(1))
.build();

RequestOptions options = RequestOptions.DEFAULT.toBuilder()
Expand All @@ -30,4 +39,3 @@ RequestOptions options = RequestOptions.DEFAULT.toBuilder()
ElasticsearchTransport transport = new Rest5ClientTransport(
restClient, new JacksonJsonpMapper(), options);
```

Loading