public interface HttpClient
extends java.lang.AutoCloseable
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"));
});
}
}
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes any pooled connections.
|
default Promise<ReceivedResponse> |
get(java.net.URI uri) |
Promise<ReceivedResponse> |
get(java.net.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.
|
ByteBufAllocator |
getByteBufAllocator()
The buffer allocator used by the client.
|
java.time.Duration |
getConnectTimeout()
The default read timeout value.
|
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 |
getPoolSize()
The number of connections that the client will pool for any given server.
|
java.time.Duration |
getReadTimeout()
The default read timeout value.
|
static HttpClient |
httpClient(ByteBufAllocator byteBufAllocator,
int maxContentLengthBytes)
Deprecated.
since 1.4, use
of(Action) |
static HttpClient |
httpClient(ServerConfig serverConfig,
Registry registry)
Deprecated.
since 1.4, use
of(Action) |
static HttpClient |
of(Action<? super HttpClientSpec> action)
Creates a new HTTP client.
|
Promise<ReceivedResponse> |
post(java.net.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(java.net.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(java.net.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.
|
static HttpClient of(Action<? super HttpClientSpec> action) throws java.lang.Exception
action
- configuration for the clientjava.lang.Exception
- any thrown by action
HttpClientSpec
Promise<ReceivedResponse> get(java.net.URI uri, Action<? super RequestSpec> action)
uri
- the request URL (as a URI), must be of the http
or https
protocolaction
- An action that will act on the RequestSpec
ReceivedResponse
default Promise<ReceivedResponse> get(java.net.URI uri)
ByteBufAllocator getByteBufAllocator()
int getPoolSize()
java.time.Duration getReadTimeout()
java.time.Duration getConnectTimeout()
int getMaxContentLength()
int getMaxResponseChunkSize()
HttpClientSpec.responseMaxChunkSize(int)
void close()
close
in interface java.lang.AutoCloseable
Promise<ReceivedResponse> post(java.net.URI uri, Action<? super RequestSpec> action)
uri
- the request URL (as a URI), must be of the http
or https
protocolaction
- An action that will act on the RequestSpec
ReceivedResponse
Promise<ReceivedResponse> request(java.net.URI uri, Action<? super RequestSpec> action)
uri
- the request URL (as a URI), must be of the http
or https
protocolaction
- An action that will act on the RequestSpec
ReceivedResponse
Promise<StreamedResponse> requestStream(java.net.URI uri, Action<? super RequestSpec> requestConfigurer)
In order to access the response content stream either subscribe to the Publisher
returned from StreamedResponse.getBody()
or use StreamedResponse.forwardTo(ratpack.http.Response, Action)
to directly stream the content as a server response.
uri
- the request URL (as a URI), must be of the http
or https
protocolrequestConfigurer
- an action that will act on the RequestSpec
StreamedResponse
StreamedResponse
@Deprecated static HttpClient httpClient(ServerConfig serverConfig, Registry registry)
of(Action)
@Deprecated static HttpClient httpClient(ByteBufAllocator byteBufAllocator, int maxContentLengthBytes)
of(Action)