Package java.net.http

Class HttpResponse.BodyHandlers

java.lang.Object
java.net.http.HttpResponse.BodyHandlers
Enclosing interface:
HttpResponse<T>

public static class HttpResponse.BodyHandlers extends Object
Implementations of BodyHandler that implement various useful handlers, such as handling the response body as a String, or streaming the response body to a file.

These implementations do not examine the status code, meaning the body is always accepted. They typically return an equivalently named BodySubscriber. Alternatively, a custom handler can be used to examine the status code and headers, and return a different body subscriber, of the same type, as appropriate.

The following are examples of using the predefined body handlers to convert a flow of response body data into common high-level Java objects:

// Receives the response body as a String
HttpResponse<String> response = client
  .send(request, BodyHandlers.ofString());
// Receives the response body as a file
HttpResponse<Path> response = client
  .send(request, BodyHandlers.ofFile(Paths.get("example.html")));
// Receives the response body as an InputStream
HttpResponse<InputStream> response = client
  .send(request, BodyHandlers.ofInputStream());
// Discards the response body
HttpResponse<Void> response = client
  .send(request, BodyHandlers.discarding());
Since:
11