Package java.net.http

Class HttpRequest.BodyPublishers

java.lang.Object
java.net.http.HttpRequest.BodyPublishers
Enclosing class:
HttpRequest

public static class HttpRequest.BodyPublishers extends Object
Implementations of BodyPublisher that implement various useful publishers, such as publishing the request body from a String, or from a file.

The following are examples of using the predefined body publishers to convert common high-level Java objects into a flow of data suitable for sending as a request body:

// Request body from a String
HttpRequest request = HttpRequest.newBuilder()
     .uri(URI.create("https://foo.com/"))
     .header("Content-Type", "text/plain; charset=UTF-8")
     .POST(BodyPublishers.ofString("some body text"))
     .build();
// Request body from a File
HttpRequest request = HttpRequest.newBuilder()
     .uri(URI.create("https://foo.com/"))
     .header("Content-Type", "application/json")
     .POST(BodyPublishers.ofFile(Paths.get("file.json")))
     .build();
// Request body from a byte array
HttpRequest request = HttpRequest.newBuilder()
     .uri(URI.create("https://foo.com/"))
     .POST(BodyPublishers.ofByteArray(new byte[] { ... }))
     .build();
Since:
11