Interface HttpClient


public interface HttpClient

An HTTP client is used to create an Endpoint representing an HTTP server and on which client-to-server HTTP exchanges are initiated.

The endpoint(java.net.InetSocketAddress) method creates endpoints bound to a the address of an HTTP server. HTTP client exchanges (request/response) are initiated from the endpoint thus obtained.

The following code show how to send a request to an HTTP server:


 HttpClient httpClient = ...;
 
 Endpoint endpoint = httpClient.endpoint("example.com". 80).build();
 
 String response = endpoint.exchange(Method.GET, "/")
 	.flatMap(Exchange::response)
 	.flatMapMany(response -> response.body().string().stream())
 	.reduceWith(() -> new StringBuilder(), (acc, chunk) -> acc.append(chunk))
 	.map(StringBuilder::toString)
  .block();
 
 endpoint.close().block();
 

UnboundExchange are created created from the client, they must be explicitly bound to an actual endpoint before they can be sent, so UnboundExchange.bind(Endpoint) must be invoked before publishers returned by Exchange.response() or Exchange.webSocket() are subscribed.

Since:
1.6
Author:
Jeremy Kuhn
  • Method Details

    • exchange

      default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange()

      Creates an unbound HTTP exchange.

      This method is a shortcut for exchange(Method.GET, "/", null).

      Returns:
      an HTTP exchange mono
    • exchange

      default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(T context)

      Creates an unbound HTTP exchange.

      This method is a shortcut for exchange(Method.GET, "/", context).

      Parameters:
      context - the exchange context
      Returns:
      an HTTP exchange mono
    • exchange

      default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(String requestTarget)

      Creates an unbound HTTP exchange.

      This method is a shortcut for exchange(Method.GET, requestTarget, null).

      Parameters:
      requestTarget - the request target path
      Returns:
      an HTTP exchange mono
    • exchange

      default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(String requestTarget, T context)

      Creates an unbound HTTP exchange.

      This method is a shortcut for exchange(Method.GET, requestTarget, context).

      Parameters:
      requestTarget - the request target path
      context - the exchange context
      Returns:
      an HTTP exchange mono
    • exchange

      default <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(Method method, String requestTarget)

      Creates an unbound HTTP exchange.

      This method is a shortcut for exchange(method, requestTarget, null).

      Parameters:
      method - the HTTP method
      requestTarget - the request target path
      Returns:
      an HTTP exchange mono
    • exchange

      <T extends ExchangeContext> Mono<? extends UnboundExchange<T>> exchange(Method method, String requestTarget, T context) throws IllegalArgumentException

      Creates an unbound HTTP exchange with a context.

      Parameters:
      method - the HTTP method
      requestTarget - the request target path
      context - the exchange context
      Returns:
      an HTTP exchange mono
      Throws:
      IllegalArgumentException - if the specified request target is invalid
    • endpoint

      default <A extends ExchangeContext> HttpClient.EndpointBuilder<A,Exchange<A>,InterceptedExchange<A>> endpoint(String host, int port)

      Creates an endpoint builder to create an Endpoint bound to the specified host and port.

      Type Parameters:
      A - the exchange context type
      Parameters:
      host - the host of the HTTP server
      port - the port of the HTTP server
      Returns:
      an endpoint builder
    • endpoint

      Creates an endpoint builder to create an Endpoint bound to specified server address.

      Type Parameters:
      A - the exchange context type
      Parameters:
      remoteAddress - the address of the HTTP server
      Returns:
      an endpoint builder