Interface HttpClient

  • All Superinterfaces:
    AutoCloseable

    public interface HttpClient
    extends 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.core.http.client.HttpClient;
     import ratpack.core.server.PublicAddress;
     import ratpack.test.embed.EmbeddedApp;
    
     import java.net.URI;
     import static org.junit.jupiter.api.Assertions.*;
    
     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 Detail

      • get

        Promise<ReceivedResponse> get​(URI uri,
                                      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
      • 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

        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

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

        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 AutoCloseable
        Since:
        1.4
      • copyWith

        HttpClient copyWith​(Action<? super HttpClientSpec> action)
                     throws 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:
        Exception - any thrown by action
        Since:
        1.6
      • post

        Promise<ReceivedResponse> post​(URI uri,
                                       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

        Promise<ReceivedResponse> request​(URI uri,
                                          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