Module methanol

Interface ResponsePayload

All Superinterfaces:
AutoCloseable

public interface ResponsePayload extends 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 Type
    Method
    Description
    <T> T
    as(TypeRef<T> typeRef)
    Converts this payload into an object of (possibly generic) type T.
    default <T> T
    as(Class<T> type)
    Converts this payload into an object of type T.
    asAsync(TypeRef<T> typeRef)
    Asynchronously converts this payload into an object of (possibly generic) type T.
    default <T> CompletableFuture<T>
    asAsync(Class<T> type)
    Asynchronously converts this payload into an object of type T.
    void
    Makes sure the resources held by this payload are released.
    <T> T
    Converts this payload using the given body handler.
    Asynchronously this payload using the given body handler.