public interface HttpClient
Promise.
All details of the request are configured by the Action acting on the RequestSpec.
Example of a simple GET and POST request.
import ratpack.http.HttpUrlBuilder;
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, action ->
action.body(body ->
body.text("foo") //Configure the POST body
)
).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 |
|---|---|
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.
|
static HttpClient |
httpClient(ByteBufAllocator byteBufAllocator,
int maxContentLengthBytes)
A method to create an instance of the default implementation of HttpClient.
|
static HttpClient |
httpClient(ServerConfig serverConfig,
Registry registry)
A method to create an instance of the default implementation of HttpClient.
|
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.
|
static HttpClient httpClient(ServerConfig serverConfig, Registry registry)
serverConfig - The ServerConfig used to provide the max content length of a response.registry - The Registry used to provide the ExecController and ByteBufAllocator needed for DefaultHttpClientstatic HttpClient httpClient(ByteBufAllocator byteBufAllocator, int maxContentLengthBytes)
byteBufAllocator - What ByteBufAllocator to use with the underlying Netty request.maxContentLengthBytes - The max content length of a response to support.Promise<ReceivedResponse> get(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 RequestSpecReceivedResponsedefault Promise<ReceivedResponse> get(URI uri)
Promise<ReceivedResponse> post(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 RequestSpecReceivedResponsePromise<ReceivedResponse> request(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 RequestSpecReceivedResponsePromise<StreamedResponse> requestStream(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, ratpack.func.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 RequestSpecStreamedResponseStreamedResponse