public abstract class HttpClient extends Object
TcpClient.connect()
is ultimately called.
Internally, materialization happens in three phases, first tcpConfiguration()
is called to retrieve a ready to use TcpClient
, then TcpClient.configure()
retrieve a usable Bootstrap
for the final TcpClient.connect()
is called.
Examples:
HttpClient.create() .baseUrl("http://example.com") .get() .response() .block();
HttpClient.create() .post() .uri("http://example.com") .send(Flux.just(bb1, bb2, bb3)) .responseSingle((res, content) -> Mono.just(res.status().code())) .block();
HttpClient.create() .baseUri("http://example.com") .post() .send(ByteBufFlux.fromString(flux)) .responseSingle((res, content) -> Mono.just(res.status().code())) .block();
Modifier and Type | Class and Description |
---|---|
static interface |
HttpClient.RedirectSendHandler
Marker interface for use with
HttpClient.RequestSender.send(BiFunction) . |
static interface |
HttpClient.RequestSender
Allow a request body configuration before calling one of the terminal,
Publisher based, HttpClient.ResponseReceiver API. |
static interface |
HttpClient.ResponseReceiver<S extends HttpClient.ResponseReceiver<?>>
A response extractor for this configured
HttpClient . |
static interface |
HttpClient.UriConfiguration<S extends HttpClient.UriConfiguration<?>>
A URI configuration
|
static interface |
HttpClient.WebsocketReceiver<S extends HttpClient.WebsocketReceiver<?>>
Allow a websocket handling.
|
static interface |
HttpClient.WebsocketSender
Allow a request body configuration before calling one of the terminal,
Publisher based, HttpClient.WebsocketReceiver API. |
Modifier and Type | Field and Description |
---|---|
static String |
USER_AGENT |
Constructor and Description |
---|
HttpClient() |
Modifier and Type | Method and Description |
---|---|
HttpClient |
addressSupplier(java.util.function.Supplier<? extends SocketAddress> connectAddressSupplier)
The address to which this client should connect for each subscribe.
|
HttpClient |
baseUrl(String baseUrl)
Configure URI to use for this request/response
|
HttpClient |
chunkedTransfer(boolean chunkedEnabled)
Deprecated.
Using
headers(Consumer) for specifying the content length
will disable the transfer-encoding |
HttpClient |
compress(boolean compressionEnabled)
Specifies whether GZip compression/websocket compression
extension is enabled.
|
HttpClient |
cookie(Cookie cookie)
Apply cookies configuration.
|
HttpClient |
cookie(String name,
java.util.function.Consumer<? super Cookie> cookieBuilder)
Apply cookies configuration.
|
HttpClient |
cookieCodec(ClientCookieEncoder encoder)
Configure the
ClientCookieEncoder , ClientCookieDecoder will be
chosen based on the encoder |
HttpClient |
cookieCodec(ClientCookieEncoder encoder,
ClientCookieDecoder decoder)
Configure the
ClientCookieEncoder and ClientCookieDecoder |
HttpClient |
cookiesWhen(String name,
java.util.function.Function<? super Cookie,Mono<? extends Cookie>> cookieBuilder)
Apply cookies configuration emitted by the returned Mono before requesting.
|
static HttpClient |
create()
Prepare a pooled
HttpClient . |
static HttpClient |
create(ConnectionProvider connectionProvider)
Prepare an
HttpClient . |
HttpClient.RequestSender |
delete()
HTTP DELETE to connect the
HttpClient . |
HttpClient |
doAfterRequest(java.util.function.BiConsumer<? super HttpClientRequest,? super Connection> doAfterRequest)
Setup a callback called when
HttpClientRequest has been sent |
HttpClient |
doAfterResponse(java.util.function.BiConsumer<? super HttpClientResponse,? super Connection> doAfterResponse)
Setup a callback called after
HttpClientResponse has been fully received. |
HttpClient |
doOnError(java.util.function.BiConsumer<? super HttpClientRequest,? super Throwable> doOnRequest,
java.util.function.BiConsumer<? super HttpClientResponse,? super Throwable> doOnResponse)
Setup a callback called when
HttpClientRequest has not been sent and when HttpClientResponse has not been fully
received. |
HttpClient |
doOnRequest(java.util.function.BiConsumer<? super HttpClientRequest,? super Connection> doOnRequest)
Setup a callback called when
HttpClientRequest is about to be sent. |
HttpClient |
doOnRequestError(java.util.function.BiConsumer<? super HttpClientRequest,? super Throwable> doOnRequest)
Setup a callback called when
HttpClientRequest has not been sent. |
HttpClient |
doOnResponse(java.util.function.BiConsumer<? super HttpClientResponse,? super Connection> doOnResponse)
Setup a callback called after
HttpClientResponse headers have been
received |
HttpClient |
doOnResponseError(java.util.function.BiConsumer<? super HttpClientResponse,? super Throwable> doOnResponse)
Setup a callback called when
HttpClientResponse has not been fully
received. |
HttpClient |
followRedirect(java.util.function.BiPredicate<HttpClientRequest,HttpClientResponse> predicate)
Enables auto-redirect support if the passed
Predicate matches. |
HttpClient |
followRedirect(java.util.function.BiPredicate<HttpClientRequest,HttpClientResponse> predicate,
java.util.function.Consumer<HttpClientRequest> redirectRequestConsumer)
Variant of
followRedirect(BiPredicate) that also accepts a redirect request
processor. |
HttpClient |
followRedirect(boolean followRedirect)
Specifies whether HTTP status 301|302|307|308 auto-redirect support is enabled.
|
HttpClient |
followRedirect(boolean followRedirect,
java.util.function.Consumer<HttpClientRequest> redirectRequestConsumer)
Variant of
followRedirect(boolean) that also accepts a redirect request
processor. |
static HttpClient |
from(TcpClient tcpClient)
Prepare a pooled
HttpClient |
HttpClient.ResponseReceiver<?> |
get()
HTTP GET to connect the
HttpClient . |
HttpClient.ResponseReceiver<?> |
head()
HTTP HEAD to connect the
HttpClient . |
HttpClient |
headers(java.util.function.Consumer<? super HttpHeaders> headerBuilder)
Apply headers configuration.
|
HttpClient |
headersWhen(java.util.function.Function<? super HttpHeaders,Mono<? extends HttpHeaders>> headerBuilder)
Apply headers configuration emitted by the returned Mono before requesting.
|
HttpClient |
httpResponseDecoder(java.util.function.Function<HttpResponseDecoderSpec,HttpResponseDecoderSpec> responseDecoderOptions)
Configure the
HttpClientCodec 's response decoding options. |
HttpClient |
keepAlive(boolean keepAlive)
Enable or Disable Keep-Alive support for the outgoing request.
|
HttpClient |
mapConnect(java.util.function.BiFunction<? super Mono<? extends Connection>,? super Bootstrap,? extends Mono<? extends Connection>> connector)
Intercept the connection lifecycle and allows to delay, transform or inject a
context.
|
static HttpClient |
newConnection()
Prepare an unpooled
HttpClient . |
HttpClient |
observe(ConnectionObserver observer)
Setup all lifecycle callbacks called on or after
Channel
has been connected and after it has been disconnected. |
HttpClient.ResponseReceiver<?> |
options()
HTTP OPTIONS to connect the
HttpClient . |
HttpClient.RequestSender |
patch()
HTTP PATCH to connect the
HttpClient . |
HttpClient |
port(int port)
The port to which this client should connect.
|
HttpClient.RequestSender |
post()
HTTP POST to connect the
HttpClient . |
HttpClient |
protocol(HttpProtocol... supportedProtocols)
The HTTP protocol to support.
|
HttpClient.RequestSender |
put()
HTTP PUT to connect the
HttpClient . |
HttpClient.RequestSender |
request(HttpMethod method)
Use the passed HTTP method to connect the
HttpClient . |
HttpClient |
secure()
Enable default sslContext support.
|
HttpClient |
secure(java.util.function.Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder)
Apply an SSL configuration customization via the passed builder.
|
protected TcpClient |
tcpConfiguration()
Get a TcpClient from the parent
HttpClient chain to use with TcpClient.connect() } or separately |
HttpClient |
tcpConfiguration(java.util.function.Function<? super TcpClient,? extends TcpClient> tcpMapper)
Apply
Bootstrap configuration given mapper taking currently configured one
and returning a new one to be ultimately used for socket binding. |
HttpClient.WebsocketSender |
websocket()
HTTP Websocket to connect the
HttpClient . |
HttpClient.WebsocketSender |
websocket(int maxFramePayloadLength)
HTTP Websocket to connect the
HttpClient . |
HttpClient.WebsocketSender |
websocket(String subprotocols)
HTTP Websocket to connect the
HttpClient . |
HttpClient.WebsocketSender |
websocket(String subprotocols,
int maxFramePayloadLength)
HTTP Websocket to connect the
HttpClient . |
HttpClient |
wiretap()
Deprecated.
Use
wiretap(boolean) |
HttpClient |
wiretap(boolean enable)
Apply or remove a wire logger configuration using
HttpClient category
and DEBUG logger level |
public static final String USER_AGENT
public static HttpClient create()
HttpClient
. HttpClient.UriConfiguration.uri(String)
or
baseUrl(String)
should be invoked before a verb
request(HttpMethod)
is selected.HttpClient
public static HttpClient create(ConnectionProvider connectionProvider)
HttpClient
. HttpClient.UriConfiguration.uri(String)
or
baseUrl(String)
should be invoked before a verb
request(HttpMethod)
is selected.HttpClient
public static HttpClient newConnection()
HttpClient
. HttpClient.UriConfiguration.uri(String)
or
baseUrl(String)
should be invoked after a verb
request(HttpMethod)
is selected.HttpClient
public static HttpClient from(TcpClient tcpClient)
HttpClient
HttpClient
public final HttpClient baseUrl(String baseUrl)
baseUrl
- a default base url that can be fully sufficient for request or can
be used to prepend future HttpClient.UriConfiguration.uri(java.lang.String)
calls.public final HttpClient addressSupplier(java.util.function.Supplier<? extends SocketAddress> connectAddressSupplier)
connectAddressSupplier
- A supplier of the address to connect to.HttpClient
public final HttpClient compress(boolean compressionEnabled)
compressionEnabled
- if true GZip compression/websocket compression extension
is enabled otherwise disabled (default: false)HttpClient
public final HttpClient mapConnect(java.util.function.BiFunction<? super Mono<? extends Connection>,? super Bootstrap,? extends Mono<? extends Connection>> connector)
connector
- A bi function mapping the default connection and configured
bootstrap to a target connection.HttpClient
@Deprecated public final HttpClient chunkedTransfer(boolean chunkedEnabled)
headers(Consumer)
for specifying the content length
will disable the transfer-encodingchunkedEnabled
- if true transfer-encoding is enabled otherwise disabled.
(default: true)HttpClient
public final HttpClient cookie(Cookie cookie)
cookie
- a cookie to append to the request(s)HttpClient
public final HttpClient cookie(String name, java.util.function.Consumer<? super Cookie> cookieBuilder)
cookieBuilder
- the header Consumer
to invoke before requestingHttpClient
public final HttpClient cookiesWhen(String name, java.util.function.Function<? super Cookie,Mono<? extends Cookie>> cookieBuilder)
cookieBuilder
- the cookies Function
to invoke before sendingHttpClient
public final HttpClient cookieCodec(ClientCookieEncoder encoder)
ClientCookieEncoder
, ClientCookieDecoder
will be
chosen based on the encoderencoder
- the preferred ClientCookieEncoderHttpClient
public final HttpClient cookieCodec(ClientCookieEncoder encoder, ClientCookieDecoder decoder)
ClientCookieEncoder
and ClientCookieDecoder
encoder
- the preferred ClientCookieEncoderdecoder
- the preferred ClientCookieDecoderHttpClient
public final HttpClient.RequestSender delete()
HttpClient
.HttpClient.RequestSender
ready to prepare the content for responsepublic final HttpClient doOnError(java.util.function.BiConsumer<? super HttpClientRequest,? super Throwable> doOnRequest, java.util.function.BiConsumer<? super HttpClientResponse,? super Throwable> doOnResponse)
HttpClientRequest
has not been sent and when HttpClientResponse
has not been fully
received.
Note that some mutation of HttpClientRequest
performed late in lifecycle
doOnRequest(BiConsumer)
or HttpClient.RequestSender.send(BiFunction)
might
not be visible if the error results from a connection failure.
doOnRequest
- a consumer observing connected eventsdoOnResponse
- a consumer observing response failuresHttpClient
public final HttpClient doOnRequest(java.util.function.BiConsumer<? super HttpClientRequest,? super Connection> doOnRequest)
HttpClientRequest
is about to be sent.doOnRequest
- a consumer observing connected eventsHttpClient
public final HttpClient doOnRequestError(java.util.function.BiConsumer<? super HttpClientRequest,? super Throwable> doOnRequest)
HttpClientRequest
has not been sent.
Note that some mutation of HttpClientRequest
performed late in lifecycle
doOnRequest(BiConsumer)
or HttpClient.RequestSender.send(BiFunction)
might
not be visible if the error results from a connection failure.doOnRequest
- a consumer observing connected eventsHttpClient
public final HttpClient doAfterRequest(java.util.function.BiConsumer<? super HttpClientRequest,? super Connection> doAfterRequest)
HttpClientRequest
has been sentdoAfterRequest
- a consumer observing connected eventsHttpClient
public final HttpClient doOnResponse(java.util.function.BiConsumer<? super HttpClientResponse,? super Connection> doOnResponse)
HttpClientResponse
headers have been
receiveddoOnResponse
- a consumer observing connected eventsHttpClient
public final HttpClient doOnResponseError(java.util.function.BiConsumer<? super HttpClientResponse,? super Throwable> doOnResponse)
HttpClientResponse
has not been fully
received.doOnResponse
- a consumer observing response failuresHttpClient
public final HttpClient doAfterResponse(java.util.function.BiConsumer<? super HttpClientResponse,? super Connection> doAfterResponse)
HttpClientResponse
has been fully received.doAfterResponse
- a consumer observing disconnected eventsHttpClient
public final HttpClient.ResponseReceiver<?> get()
HttpClient
.HttpClient.RequestSender
ready to consume for responsepublic final HttpClient.ResponseReceiver<?> head()
HttpClient
.HttpClient.RequestSender
ready to consume for responsepublic final HttpClient headers(java.util.function.Consumer<? super HttpHeaders> headerBuilder)
headerBuilder
- the header Consumer
to invoke before requestingHttpClient
public final HttpClient headersWhen(java.util.function.Function<? super HttpHeaders,Mono<? extends HttpHeaders>> headerBuilder)
headerBuilder
- the header Function
to invoke before sendingHttpClient
public final HttpClient keepAlive(boolean keepAlive)
keepAlive
- true if keepAlive should be enabled (default: true)HttpClient
public final HttpClient followRedirect(boolean followRedirect)
followRedirect
- if true HTTP status 301|302|307|308 auto-redirect support
is enabled, otherwise disabled (default: false).HttpClient
public final HttpClient followRedirect(boolean followRedirect, @Nullable java.util.function.Consumer<HttpClientRequest> redirectRequestConsumer)
followRedirect(boolean)
that also accepts a redirect request
processor.followRedirect
- if true HTTP status 301|302|307|308 auto-redirect support
is enabled, otherwise disabled (default: false).redirectRequestConsumer
- redirect request consumer, invoked on redirects, after
the redirect request has been initialized, in order to apply further changes such as
add/remove headers and cookies; use HttpClientRequest.redirectedFrom()
to
check the original and any number of subsequent redirect(s), including the one that
is in progress.HttpClient
public final HttpClient followRedirect(java.util.function.BiPredicate<HttpClientRequest,HttpClientResponse> predicate)
Predicate
matches.
note the passed HttpClientRequest
and HttpClientResponse
should be considered read-only and the implement SHOULD NOT consume or
write the request/response in this predicate.
predicate
- that returns true to enable auto-redirect support.HttpClient
public final HttpClient followRedirect(java.util.function.BiPredicate<HttpClientRequest,HttpClientResponse> predicate, @Nullable java.util.function.Consumer<HttpClientRequest> redirectRequestConsumer)
followRedirect(BiPredicate)
that also accepts a redirect request
processor.predicate
- that returns true to enable auto-redirect support.redirectRequestConsumer
- redirect request consumer, invoked on redirects, after
the redirect request has been initialized, in order to apply further changes such as
add/remove headers and cookies; use HttpClientRequest.redirectedFrom()
to
check the original and any number of subsequent redirect(s), including the one that
is in progress.HttpClient
public final HttpClient observe(ConnectionObserver observer)
Channel
has been connected and after it has been disconnected.observer
- a consumer observing state changesHttpClient
public final HttpClient.ResponseReceiver<?> options()
HttpClient
.HttpClient.RequestSender
ready to consume for responsepublic final HttpClient.RequestSender patch()
HttpClient
.HttpClient.RequestSender
ready to finalize request and consume for responsepublic final HttpClient port(int port)
port
- The port to connect to.HttpClient
public final HttpClient.RequestSender post()
HttpClient
.HttpClient.RequestSender
ready to finalize request and consume for responsepublic final HttpClient protocol(HttpProtocol... supportedProtocols)
HttpProtocol.HTTP11
.supportedProtocols
- The various HttpProtocol
this server will supportHttpClient
public final HttpClient.RequestSender put()
HttpClient
.HttpClient.RequestSender
ready to finalize request and consume for responsepublic HttpClient.RequestSender request(HttpMethod method)
HttpClient
.method
- the HTTP method to sendHttpClient.RequestSender
ready to finalize request and consume for responsepublic final HttpClient secure()
SslContext
will be
assigned to
with a default value of 10
seconds handshake timeout unless
the environment property reactor.netty.tcp.sslHandshakeTimeout
is set.HttpClient
public final HttpClient secure(java.util.function.Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder)
SslContext
to be passed to with a default value of
10
seconds handshake timeout unless the environment property reactor.netty.tcp.sslHandshakeTimeout
is set.sslProviderBuilder
- builder callback for further customization of SslContext.HttpClient
public final HttpClient tcpConfiguration(java.util.function.Function<? super TcpClient,? extends TcpClient> tcpMapper)
Bootstrap
configuration given mapper taking currently configured one
and returning a new one to be ultimately used for socket binding. Configuration
will apply during tcpConfiguration()
phase.
Always prefer secure()
to
tcpConfiguration()
and TcpClient.secure()
. While configuration
with the later is possible, secure()
will inject extra information
for HTTPS support.
tcpMapper
- A tcpClient mapping function to update tcp configuration and
return an enriched tcp client to use.HttpClient
@Deprecated public final HttpClient wiretap()
wiretap(boolean)
HttpClient
category
and DEBUG
logger levelHttpClient
public final HttpClient wiretap(boolean enable)
HttpClient
category
and DEBUG
logger levelenable
- Specifies whether the wire logger configuration will be added to
the pipelineHttpClient
public final HttpClient httpResponseDecoder(java.util.function.Function<HttpResponseDecoderSpec,HttpResponseDecoderSpec> responseDecoderOptions)
HttpClientCodec
's response decoding options.responseDecoderOptions
- a function to mutate the provided Http response decoder optionsHttpClient
public final HttpClient.WebsocketSender websocket()
HttpClient
.HttpClient.WebsocketSender
ready to consume for responsepublic final HttpClient.WebsocketSender websocket(String subprotocols)
HttpClient
.subprotocols
- a websocket subprotocol comma separated listHttpClient.WebsocketSender
ready to consume for responsepublic final HttpClient.WebsocketSender websocket(int maxFramePayloadLength)
HttpClient
.maxFramePayloadLength
- maximum allowable frame payload lengthHttpClient.WebsocketSender
ready to consume for responsepublic final HttpClient.WebsocketSender websocket(String subprotocols, int maxFramePayloadLength)
HttpClient
.subprotocols
- a websocket subprotocol comma separated listmaxFramePayloadLength
- maximum allowable frame payload lengthHttpClient.WebsocketSender
ready to consume for responseprotected TcpClient tcpConfiguration()
HttpClient
chain to use with TcpClient.connect()
} or separatelyTcpClient