Package ratpack.core.http.client
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 Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
close()
Closes any pooled connections.HttpClient
copyWith(Action<? super HttpClientSpec> action)
Create a new HttpClient by appending the provided configuration to this client.default Promise<ReceivedResponse>
get(URI uri)
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.io.netty.buffer.ByteBufAllocator
getByteBufAllocator()
The buffer allocator used by the client.Duration
getConnectTimeout()
The default read timeout value.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.Duration
getReadTimeout()
The default read timeout value.static HttpClient
of(Action<? super HttpClientSpec> action)
Creates a new HTTP client.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.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.Promise<StreamedResponse>
requestStream(URI uri, 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(Action<? super HttpClientSpec> action) throws Exception
Creates a new HTTP client.- Parameters:
action
- configuration for the client- Returns:
- a HTTP client
- Throws:
Exception
- any thrown byaction
- Since:
- 1.4
- See Also:
HttpClientSpec
-
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 thehttp
orhttps
protocolaction
- An action that will act on theRequestSpec
- Returns:
- A promise for a
ReceivedResponse
-
get
default Promise<ReceivedResponse> get(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
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 interfaceAutoCloseable
- 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 byaction
- 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 thehttp
orhttps
protocolaction
- An action that will act on theRequestSpec
- 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 thehttp
orhttps
protocolaction
- An action that will act on theRequestSpec
- Returns:
- A promise for a
ReceivedResponse
-
requestStream
Promise<StreamedResponse> requestStream(URI uri, 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.In order to access the response content stream either subscribe to the
Publisher
returned fromStreamedResponse.getBody()
or useStreamedResponse.forwardTo(Response, Action)
to directly stream the content as a server response.- Parameters:
uri
- the request URL (as a URI), must be of thehttp
orhttps
protocolrequestConfigurer
- an action that will act on theRequestSpec
- Returns:
- a promise for a
StreamedResponse
- See Also:
StreamedResponse
-
-