- All Superinterfaces:
AutoCloseable
A response body that is yet to be handled into the desirable type (see
Methanol.send(HttpRequest)
).
To avoid resource leaks, it is recommended to use this within a try-with-resources construct immediately after sending the response (regardless of whether the body is converted synchronously or asynchronously):
var response = client.send(MutableRequest.GET("https://example.com"));
try(var body = response.body()) {
if (HttpStatus.isSuccessful(response) {
System.out.println(body.as(SuccessType.class));
} else if (HttpStatus.isClientError(response)) {
System.out.println(body.as(ErrorType.class));
} else {
// Discard body.
}
}
-
Method Summary
Modifier and TypeMethodDescription<T> T
Converts this payload into an object of (possibly generic) typeT
.default <T> T
Converts this payload into an object of typeT
.<T> CompletableFuture
<T> Asynchronously converts this payload into an object of (possibly generic) typeT
.default <T> CompletableFuture
<T> Asynchronously converts this payload into an object of typeT
.void
close()
Makes sure the resources held by this payload are released.<T> T
with
(HttpResponse.BodyHandler<T> bodyHandler) Converts this payload using the given body handler.<T> CompletableFuture
<T> withAsync
(HttpResponse.BodyHandler<T> bodyHandler) Asynchronously this payload using the given body handler.
-
Method Details
-
as
Converts this payload into an object of typeT
.- Throws:
IOException
InterruptedException
-
as
Converts this payload into an object of (possibly generic) typeT
.- Throws:
IOException
InterruptedException
-
with
Converts this payload using the given body handler.- Throws:
IOException
InterruptedException
-
asAsync
Asynchronously converts this payload into an object of typeT
. -
asAsync
Asynchronously converts this payload into an object of (possibly generic) typeT
. -
withAsync
Asynchronously this payload using the given body handler. -
close
void close()Makes sure the resources held by this payload are released. If the payload has been consumed, this method does nothing. Otherwise, the response body is discarded.- Specified by:
close
in interfaceAutoCloseable
-