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.client.HttpClient;
import ratpack.server.PublicAddress;
import ratpack.test.embed.EmbeddedApp;
import java.net.URI;
public class ExampleHttpClient {
public static void main(String[] args) throws Exception {
EmbeddedApp.fromHandlers(chain -> {
chain
.get("/simpleGet", context -> {
PublicAddress address = context.get(PublicAddress.class); //find local ip address
HttpClient httpClient = context.get(HttpClient.class); //get httpClient
httpClient.get(new URI(address.getAddress(context).toString() + "/httpClientGet")).then(response -> {
context.render(response.getBody().getText()); //Render the response from the httpClient GET request
}
);
})
.get("/simplePost", context -> {
PublicAddress address = context.get(PublicAddress.class); //find local ip address
HttpClient httpClient = context.get(HttpClient.class); //get httpClient
httpClient.post(new URI(address.getAddress(context).toString() + "/httpClientPost"), action ->
action.body(body ->
body.text("foo") //Configure the POST body
)
).then(response -> {
context.render(response.getBody().getText()); //Render the response from the httpClient POST request
});
})
.get("httpClientGet", context -> {
context.render("httpClientGet");
})
.post("httpClientPost", context -> {
context.render(context.getRequest().getBody().getText().toUpperCase());
});
}
).test(testHttpClient -> {
assert testHttpClient.get("/simpleGet").getBody().getText().equals("httpClientGet");
assert testHttpClient.get("/simplePost").getBody().getText().equals("FOO");
});
}
}
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(ExecController execController,
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(ExecController execController, ByteBufAllocator byteBufAllocator, int maxContentLengthBytes)
execController
- The ExecController used while making the requests.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 RequestSpec
ReceivedResponse
default 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 RequestSpec
ReceivedResponse
Promise<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 RequestSpec
ReceivedResponse
Promise<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.send(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 RequestSpec
StreamedResponse
StreamedResponse