io.vertx.core.http

## Interface HttpClient

- All Superinterfaces:
    Measured

      public interface HttpClient
      extends Measured

  An asynchronous HTTP client.

  It allows you to make requests to HTTP servers, and a single client can make requests to any server.

  It also allows you to open WebSockets to servers.

  The client can also pool HTTP connections.

  For pooling to occur, keep-alive must be true on the `HttpClientOptions` (default is true). In this case connections will be pooled and re-used if there are pending HTTP requests waiting to get a connection, otherwise they will be closed.

  This gives the benefits of keep alive when the client is loaded but means we don't keep connections hanging around unnecessarily when there would be no benefits anyway.

  The client also supports pipe-lining of requests. Pipe-lining means another request is sent on the same connection before the response from the preceding one has returned. Pipe-lining is not appropriate for all requests.

  To enable pipe-lining, it must be enabled on the `HttpClientOptions` (default is false).

  When pipe-lining is enabled the connection will be automatically closed when all in-flight responses have returned and there are no outstanding pending requests to write.

  The client is designed to be reused between requests.

  Author:
    Tim Fox

- - ### Method Summary

    All Methods Instance Methods Abstract Methods
    Modifier and Type | Method and Description
    --- | ---
    `Future<Void>` | `close()` Like `close(Handler)` but returns a `Future` of the asynchronous result
    `void` | `close(Handler<AsyncResult<Void>> handler)` Close the client.
    `HttpClient` | `connectionHandler(Handler<HttpConnection> handler)` Set a connection handler for the client.
    `java.util.function.Function<HttpClientResponse,Future<RequestOptions>>` | `redirectHandler()`
    `HttpClient` | `redirectHandler(java.util.function.Function<HttpClientResponse,Future<RequestOptions>> handler)` Set a redirect handler for the http client.
    `Future<HttpClientRequest>` | `request(HttpMethod method, int port, String host, String requestURI)` Like `request(HttpMethod, int, String, String, Handler)` but returns a `Future` of the asynchronous result
    `void` | `request(HttpMethod method, int port, String host, String requestURI, Handler<AsyncResult<HttpClientRequest>> handler)` Create an HTTP request to send to the server at the `host` and `port`.
    `Future<HttpClientRequest>` | `request(HttpMethod method, String requestURI)` Like `request(HttpMethod, String, Handler)` but returns a `Future` of the asynchronous result
    `void` | `request(HttpMethod method, String requestURI, Handler<AsyncResult<HttpClientRequest>> handler)` Create an HTTP request to send to the server at the default host and port.
    `Future<HttpClientRequest>` | `request(HttpMethod method, String host, String requestURI)` Like `request(HttpMethod, String, String, Handler)` but returns a `Future` of the asynchronous result
    `void` | `request(HttpMethod method, String host, String requestURI, Handler<AsyncResult<HttpClientRequest>> handler)` Create an HTTP request to send to the server at the `host` and default port.
    `Future<HttpClientRequest>` | `request(RequestOptions options)` Like `request(RequestOptions, Handler)` but returns a `Future` of the asynchronous result
    `void` | `request(RequestOptions options, Handler<AsyncResult<HttpClientRequest>> handler)` Create an HTTP request to send to the server.
    `Future<WebSocket>` | `webSocket(int port, String host, String requestURI)` Like `webSocket(int, String, String, Handler)` but returns a `Future` of the asynchronous result
    `void` | `webSocket(int port, String host, String requestURI, Handler<AsyncResult<WebSocket>> handler)` Connect a WebSocket to the specified port, host and relative request URI
    `Future<WebSocket>` | `webSocket(String requestURI)` Like `webSocket(String, Handler)` but returns a `Future` of the asynchronous result
    `void` | `webSocket(String requestURI, Handler<AsyncResult<WebSocket>> handler)` Connect a WebSocket at the relative request URI using the default host and port
    `Future<WebSocket>` | `webSocket(String host, String requestURI)` Like `webSocket(String, String, Handler)` but returns a `Future` of the asynchronous result
    `void` | `webSocket(String host, String requestURI, Handler<AsyncResult<WebSocket>> handler)` Connect a WebSocket to the host and relative request URI and default port
    `Future<WebSocket>` | `webSocket(WebSocketConnectOptions options)` Like `webSocket(WebSocketConnectOptions, Handler)` but returns a `Future` of the asynchronous result
    `void` | `webSocket(WebSocketConnectOptions options, Handler<AsyncResult<WebSocket>> handler)` Connect a WebSocket with the specified options.
    `Future<WebSocket>` | `webSocketAbs(String url, MultiMap headers, WebsocketVersion version, List<String> subProtocols)` Like `webSocketAbs(String, MultiMap, WebsocketVersion, List, Handler)` but returns a `Future` of the asynchronous result
    `void` | `webSocketAbs(String url, MultiMap headers, WebsocketVersion version, List<String> subProtocols, Handler<AsyncResult<WebSocket>> handler)` Connect a WebSocket with the specified absolute url, with the specified headers, using the specified version of WebSockets, and the specified WebSocket sub protocols.

    - ### Methods inherited from interface io.vertx.core.metrics.Measured

      `isMetricsEnabled`

- - ### Method Detail

    - #### request

          void request(RequestOptions options,
                       Handler<AsyncResult<HttpClientRequest>> handler)

      Create an HTTP request to send to the server. The `handler` is called when the request is ready to be sent.
      Parameters:
        `options` - the request options
        `handler` - the handler called when the request is ready to be sent

    - #### request

          Future<HttpClientRequest> request(RequestOptions options)

      Like `request(RequestOptions, Handler)` but returns a `Future` of the asynchronous result

    - #### request

          void request(HttpMethod method,
                       int port,
                       String host,
                       String requestURI,
                       Handler<AsyncResult<HttpClientRequest>> handler)

      Create an HTTP request to send to the server at the `host` and `port`. The `handler` is called when the request is ready to be sent.
      Parameters:
        `method` - the HTTP method
        `port` - the port
        `host` - the host
        `requestURI` - the relative URI
        `handler` - the handler called when the request is ready to be sent

    - #### request

          Future<HttpClientRequest> request(HttpMethod method,
                                            int port,
                                            String host,
                                            String requestURI)

      Like `request(HttpMethod, int, String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### request

          void request(HttpMethod method,
                       String host,
                       String requestURI,
                       Handler<AsyncResult<HttpClientRequest>> handler)

      Create an HTTP request to send to the server at the `host` and default port. The `handler` is called when the request is ready to be sent.
      Parameters:
        `method` - the HTTP method
        `host` - the host
        `requestURI` - the relative URI
        `handler` - the handler called when the request is ready to be sent

    - #### request

          Future<HttpClientRequest> request(HttpMethod method,
                                            String host,
                                            String requestURI)

      Like `request(HttpMethod, String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### request

          void request(HttpMethod method,
                       String requestURI,
                       Handler<AsyncResult<HttpClientRequest>> handler)

      Create an HTTP request to send to the server at the default host and port. The `handler` is called when the request is ready to be sent.
      Parameters:
        `method` - the HTTP method
        `requestURI` - the relative URI
        `handler` - the handler called when the request is ready to be sent

    - #### request

          Future<HttpClientRequest> request(HttpMethod method,
                                            String requestURI)

      Like `request(HttpMethod, String, Handler)` but returns a `Future` of the asynchronous result

    - #### webSocket

          void webSocket(int port,
                         String host,
                         String requestURI,
                         Handler<AsyncResult<WebSocket>> handler)

      Connect a WebSocket to the specified port, host and relative request URI
      Parameters:
        `port` - the port
        `host` - the host
        `requestURI` - the relative URI
        `handler` - handler that will be called with the WebSocket when connected

    - #### webSocket

          Future<WebSocket> webSocket(int port,
                                      String host,
                                      String requestURI)

      Like `webSocket(int, String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### webSocket

          void webSocket(String host,
                         String requestURI,
                         Handler<AsyncResult<WebSocket>> handler)

      Connect a WebSocket to the host and relative request URI and default port
      Parameters:
        `host` - the host
        `requestURI` - the relative URI
        `handler` - handler that will be called with the WebSocket when connected

    - #### webSocket

          Future<WebSocket> webSocket(String host,
                                      String requestURI)

      Like `webSocket(String, String, Handler)` but returns a `Future` of the asynchronous result

    - #### webSocket

          void webSocket(String requestURI,
                         Handler<AsyncResult<WebSocket>> handler)

      Connect a WebSocket at the relative request URI using the default host and port
      Parameters:
        `requestURI` - the relative URI
        `handler` - handler that will be called with the WebSocket when connected

    - #### webSocket

          Future<WebSocket> webSocket(String requestURI)

      Like `webSocket(String, Handler)` but returns a `Future` of the asynchronous result

    - #### webSocket

          void webSocket(WebSocketConnectOptions options,
                         Handler<AsyncResult<WebSocket>> handler)

      Connect a WebSocket with the specified options.
      Parameters:
        `options` - the request options

    - #### webSocket

          Future<WebSocket> webSocket(WebSocketConnectOptions options)

      Like `webSocket(WebSocketConnectOptions, Handler)` but returns a `Future` of the asynchronous result

    - #### webSocketAbs

          void webSocketAbs(String url,
                            MultiMap headers,
                            WebsocketVersion version,
                            List<String> subProtocols,
                            Handler<AsyncResult<WebSocket>> handler)

      Connect a WebSocket with the specified absolute url, with the specified headers, using the specified version of WebSockets, and the specified WebSocket sub protocols.
      Parameters:
        `url` - the absolute url
        `headers` - the headers
        `version` - the WebSocket version
        `subProtocols` - the subprotocols to use
        `handler` - handler that will be called if WebSocket connection fails

    - #### webSocketAbs

          Future<WebSocket> webSocketAbs(String url,
                                         MultiMap headers,
                                         WebsocketVersion version,
                                         List<String> subProtocols)

      Like `webSocketAbs(String, MultiMap, WebsocketVersion, List, Handler)` but returns a `Future` of the asynchronous result

    - #### connectionHandler

          HttpClient connectionHandler(Handler<HttpConnection> handler)

      Set a connection handler for the client. This handler is called when a new connection is established.
      Returns:
        a reference to this, so the API can be used fluently

    - #### redirectHandler

          HttpClient redirectHandler(java.util.function.Function<HttpClientResponse,Future<RequestOptions>> handler)

      Set a redirect handler for the http client.

      The redirect handler is called when a `3xx` response is received and the request is configured to follow redirects with `HttpClientRequest.setFollowRedirects(boolean)`.

      The redirect handler is passed the `HttpClientResponse`, it can return an `HttpClientRequest` or `null`.

      - when null is returned, the original response is processed by the original request response handler
      - when a new `Future<HttpClientRequest>` is returned, the client will send this new request
      The new request will get a copy of the previous request headers unless headers are set. In this case, the client assumes that the redirect handler exclusively managers the headers of the new request.

      The handler must return a `Future<HttpClientRequest>` unsent so the client can further configure it and send it.

      Parameters:
        `handler` - the new redirect handler
      Returns:
        a reference to this, so the API can be used fluently

    - #### redirectHandler

          java.util.function.Function<HttpClientResponse,Future<RequestOptions>> redirectHandler()

      Returns:
        the current redirect handler.

    - #### close

          void close(Handler<AsyncResult<Void>> handler)

      Close the client. Closing will close down any pooled connections. Clients should always be closed after use.

    - #### close

          Future<Void> close()

      Like `close(Handler)` but returns a `Future` of the asynchronous result

Copyright © 2022 Eclipse. All rights reserved.