Interface HttpClient
- All Known Implementing Classes:
ApacheHttpClient, JdkHttpClient, LoggingHttpClient, OkHttpClient
public interface HttpClient
A client for executing HTTP requests both synchronously and asynchronously.
This interface is currently experimental and subject to change.
-
Method Summary
Modifier and TypeMethodDescriptionexecute(HttpRequest request) Executes a given HTTP request synchronously and returns the response.default voidexecute(HttpRequest request, ServerSentEventListener listener) Executes a given HTTP request asynchronously with server-sent events (SSE) handling.voidexecute(HttpRequest request, ServerSentEventParser parser, ServerSentEventListener listener) Executes a given HTTP request asynchronously with server-sent events (SSE) handling.default CompletableFuture<SuccessfulHttpResponse> executeAsync(HttpRequest request) Non-blocking counterpart ofexecute(HttpRequest).default Flow.Publisher<HttpStreamingEvent> stream(HttpRequest request) Executes a streaming HTTP request and exposes the parsed events as a coldFlow.PublisherofHttpStreamingEvents.default Flow.Publisher<HttpStreamingEvent> stream(HttpRequest request, ServerSentEventParser parser) Likestream(HttpRequest), but with a caller-suppliedServerSentEventParser.
-
Method Details
-
execute
Executes a given HTTP request synchronously and returns the response. This method blocks until the entire response is received.- Parameters:
request- the HTTP request to be executed.- Returns:
- a
SuccessfulHttpResponsecontaining the response data for successful HTTP requests (2XX status codes) - Throws:
HttpException- if the server returns a client (4XX) or server (5XX) error responseRuntimeException- if an unexpected error occurs during request execution (e.g., network issues, timeouts)
-
executeAsync
Non-blocking counterpart ofexecute(HttpRequest). Returns immediately with aCompletableFuturethat completes with theSuccessfulHttpResponseonce the full response has been received, without blocking the calling thread. The future completes exceptionally with anHttpExceptionfor non-2XX responses, or with the underlying error (e.g. a timeout or network failure) otherwise.- Parameters:
request- the HTTP request to be executed.- Returns:
- a
CompletableFutureof theSuccessfulHttpResponse. - Since:
- 1.20.0
-
execute
Executes a given HTTP request asynchronously with server-sent events (SSE) handling. This method returns immediately while processing continues on a separate thread. Events are processed through the providedServerSentEventListener.The execution flow is as follows:
- The request is initiated asynchronously
- Received SSE data is parsed using the
DefaultServerSentEventParser - Parsed events are delivered to the listener's appropriate methods
- If an error occurs,
ServerSentEventListener.onError(Throwable)is called
If any exception is thrown from the listener's methods, the stream processing will be terminated and no further events will be processed.
- Parameters:
request- the HTTP request to be executed.listener- the listener to receive parsed events and error notifications.
-
execute
Executes a given HTTP request asynchronously with server-sent events (SSE) handling. This method returns immediately while processing continues on a separate thread. Events are processed through the providedServerSentEventListener.The execution flow is as follows:
- The request is initiated asynchronously
- Received SSE data is parsed using the provided parser
- Parsed events are delivered to the listener's appropriate methods
- If an error occurs,
ServerSentEventListener.onError(Throwable)is called
If any exception is thrown from the listener's methods, the stream processing will be terminated and no further events will be processed.
- Parameters:
request- the HTTP request to be executed.parser- the parser to process incoming server-sent events.listener- the listener to receive parsed events and error notifications.
-
stream
Executes a streaming HTTP request and exposes the parsed events as a coldFlow.PublisherofHttpStreamingEvents. Nothing happens until you subscribe: eachsubscribe()initiates a new request.This interface gives no guarantee about thread-pinning or whether events are delivered incrementally; such guarantees depend on the implementation. Consult the chosen implementation's javadoc.
Uses
DefaultServerSentEventParserfor SSE parsing.- Since:
- 1.20.0
-
stream
@Experimental default Flow.Publisher<HttpStreamingEvent> stream(HttpRequest request, ServerSentEventParser parser) Likestream(HttpRequest), but with a caller-suppliedServerSentEventParser.- Since:
- 1.20.0
-