Interface HttpClient

  • All Superinterfaces:
    java.lang.AutoCloseable

    public interface HttpClient
    extends java.lang.AutoCloseable
    An asynchronous HTTP client.

    A default instance is always available in an application through the server registry. The default instance does not use connection pooling and has conservative defaults. Alternative instances can be created via of(Action).

    
     import ratpack.http.client.HttpClient;
     import ratpack.server.PublicAddress;
     import ratpack.test.embed.EmbeddedApp;
    
     import java.net.URI;
     import static org.junit.Assert.*;
    
     public class ExampleHttpClient {
    
       public static void main(String... args) throws Exception {
         EmbeddedApp.fromHandlers(chain -> {
             chain
               .get("simpleGet", ctx -> {
                 PublicAddress address = ctx.get(PublicAddress.class);         //find local ip address
                 HttpClient httpClient = ctx.get(HttpClient.class);            //get httpClient
                 URI uri = address.get("httpClientGet");
    
                 httpClient.get(uri).then(response ->
                     ctx.render(response.getBody().getText())  //Render the response from the httpClient GET request
                 );
               })
               .get("simplePost", ctx -> {
                 PublicAddress address = ctx.get(PublicAddress.class);  //find local ip address
                 HttpClient httpClient = ctx.get(HttpClient.class);     //get httpClient
                 URI uri = address.get("httpClientPost");
    
                 httpClient.post(uri, s -> s.getBody().text("foo")).then(response ->
                   ctx.render(response.getBody().getText())   //Render the response from the httpClient POST request
                 );
               })
               .get("httpClientGet", ctx -> ctx.render("httpClientGet"))
               .post("httpClientPost", ctx -> ctx.render(ctx.getRequest().getBody().map(b -> b.getText().toUpperCase())));
           }
         ).test(testHttpClient -> {
           assertEquals("httpClientGet", testHttpClient.getText("/simpleGet"));
           assertEquals("FOO", testHttpClient.getText("/simplePost"));
         });
       }
     }
    
     
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods 
      Modifier and Type Method Description
      void close()
      Closes any pooled connections.
      HttpClient copyWith​(ratpack.func.Action<? super HttpClientSpec> action)
      Create a new HttpClient by appending the provided configuration to this client.
      default ratpack.exec.Promise<ReceivedResponse> get​(java.net.URI uri)  
      ratpack.exec.Promise<ReceivedResponse> get​(java.net.URI uri, ratpack.func.Action<? super RequestSpec> action)
      An asynchronous method to do a GET HTTP request, the URL and all details of the request are configured by the Action acting on the RequestSpec, but the method will be defaulted to a GET.
      io.netty.buffer.ByteBufAllocator getByteBufAllocator()
      The buffer allocator used by the client.
      java.time.Duration getConnectTimeout()
      The default read timeout value.
      java.time.Duration getIdleTimeout()
      The idle connect timeout for connections in the connection pool, after which the the offending channel will be closed.
      int getMaxContentLength()
      The maximum response length accepted by the client.
      int getMaxResponseChunkSize()
      The max size of the chunks to emit when reading a response as a stream.
      int getPoolQueueSize()
      The number of connections that the client will queue if pool was depleted for any given server.
      int getPoolSize()
      The number of connections that the client will pool for any given server.
      Proxy getProxy()
      The configured proxy instance for the client.
      java.time.Duration getReadTimeout()
      The default read timeout value.
      static HttpClient httpClient​(io.netty.buffer.ByteBufAllocator byteBufAllocator, int maxContentLengthBytes)
      Deprecated.
      since 1.4, use of(Action)
      static HttpClient httpClient​(ServerConfig serverConfig, ratpack.registry.Registry registry)
      Deprecated.
      since 1.4, use of(Action)
      static HttpClient of​(ratpack.func.Action<? super HttpClientSpec> action)
      Creates a new HTTP client.
      ratpack.exec.Promise<ReceivedResponse> post​(java.net.URI uri, ratpack.func.Action<? super RequestSpec> action)
      An asynchronous method to do a POST HTTP request, the URL and all details of the request are configured by the Action acting on the RequestSpec, but the method will be defaulted to a POST.
      ratpack.exec.Promise<ReceivedResponse> request​(java.net.URI uri, ratpack.func.Action<? super RequestSpec> action)
      An asynchronous method to do a HTTP request, the URL and all details of the request are configured by the Action acting on the RequestSpec.
      ratpack.exec.Promise<StreamedResponse> requestStream​(java.net.URI uri, ratpack.func.Action<? super RequestSpec> requestConfigurer)
      An asynchronous method to do a HTTP request, the URL and all details of the request are configured by the Action acting on the RequestSpec, the received response content will be streamed.
    • Method Detail

      • of

        static HttpClient of​(ratpack.func.Action<? super HttpClientSpec> action)
                      throws java.lang.Exception
        Creates a new HTTP client.
        Parameters:
        action - configuration for the client
        Returns:
        a HTTP client
        Throws:
        java.lang.Exception - any thrown by action
        Since:
        1.4
        See Also:
        HttpClientSpec
      • get

        ratpack.exec.Promise<ReceivedResponse> get​(java.net.URI uri,
                                                   ratpack.func.Action<? super RequestSpec> action)
        An asynchronous method to do a GET HTTP request, the URL and all details of the request are configured by the Action acting on the RequestSpec, but the method will be defaulted to a GET.
        Parameters:
        uri - the request URL (as a URI), must be of the http or https protocol
        action - An action that will act on the RequestSpec
        Returns:
        A promise for a ReceivedResponse
      • get

        default ratpack.exec.Promise<ReceivedResponse> get​(java.net.URI uri)
      • getByteBufAllocator

        io.netty.buffer.ByteBufAllocator getByteBufAllocator()
        The buffer allocator used by the client.
        Since:
        1.4
      • getPoolSize

        int getPoolSize()
        The number of connections that the client will pool for any given server.
        Since:
        1.4
      • getPoolQueueSize

        int getPoolQueueSize()
        The number of connections that the client will queue if pool was depleted for any given server.
        Since:
        1.6
      • getIdleTimeout

        java.time.Duration getIdleTimeout()
        The idle connect timeout for connections in the connection pool, after which the the offending channel will be closed.

        If not set, the default is 0, indicating no timeout.

        Since:
        1.7
      • getReadTimeout

        java.time.Duration getReadTimeout()
        The default read timeout value.
        Since:
        1.4
      • getConnectTimeout

        java.time.Duration getConnectTimeout()
        The default read timeout value.
        Since:
        1.5
      • getMaxContentLength

        int getMaxContentLength()
        The maximum response length accepted by the client.
        Since:
        1.4
      • getMaxResponseChunkSize

        int getMaxResponseChunkSize()
        The max size of the chunks to emit when reading a response as a stream.
        Returns:
        The max size of the chunks to emit when reading a response as a stream
        Since:
        1.5
        See Also:
        HttpClientSpec.responseMaxChunkSize(int)
      • getProxy

        Proxy getProxy()
        The configured proxy instance for the client.
        Returns:
        The configure proxy instance for the client
        Since:
        1.8.0
      • close

        void close()
        Closes any pooled connections.
        Specified by:
        close in interface java.lang.AutoCloseable
        Since:
        1.4
      • copyWith

        HttpClient copyWith​(ratpack.func.Action<? super HttpClientSpec> action)
                     throws java.lang.Exception
        Create a new HttpClient by appending the provided configuration to this client.
        Parameters:
        action - The additional configuration to apply to the new client
        Returns:
        a http client
        Throws:
        java.lang.Exception - any thrown by action
        Since:
        1.6
      • post

        ratpack.exec.Promise<ReceivedResponse> post​(java.net.URI uri,
                                                    ratpack.func.Action<? super RequestSpec> action)
        An asynchronous method to do a POST HTTP request, the URL and all details of the request are configured by the Action acting on the RequestSpec, but the method will be defaulted to a POST.
        Parameters:
        uri - the request URL (as a URI), must be of the http or https protocol
        action - An action that will act on the RequestSpec
        Returns:
        A promise for a ReceivedResponse
      • request

        ratpack.exec.Promise<ReceivedResponse> request​(java.net.URI uri,
                                                       ratpack.func.Action<? super RequestSpec> action)
        An asynchronous method to do a HTTP request, the URL and all details of the request are configured by the Action acting on the RequestSpec.
        Parameters:
        uri - the request URL (as a URI), must be of the http or https protocol
        action - An action that will act on the RequestSpec
        Returns:
        A promise for a ReceivedResponse
      • httpClient

        @Deprecated
        static HttpClient httpClient​(ServerConfig serverConfig,
                                     ratpack.registry.Registry registry)
        Deprecated.
        since 1.4, use of(Action)
      • httpClient

        @Deprecated
        static HttpClient httpClient​(io.netty.buffer.ByteBufAllocator byteBufAllocator,
                                     int maxContentLengthBytes)
        Deprecated.
        since 1.4, use of(Action)