HttpRequestpublic static class HttpRequest.BodyPublishers extends Object
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();| Modifier and Type | Method | Description | 
|---|---|---|
| static HttpRequest.BodyPublisher | concat | Returns a  BodyPublisherthat publishes a request body consisting of the concatenation of the request bodies published by a sequence of publishers. | 
| static HttpRequest.BodyPublisher | fromPublisher | Returns a request body publisher whose body is retrieved from the given  Flow.Publisher. | 
| static HttpRequest.BodyPublisher | fromPublisher | Returns a request body publisher whose body is retrieved from the given  Flow.Publisher. | 
| static HttpRequest.BodyPublisher | noBody() | A request body publisher which sends no request body. | 
| static HttpRequest.BodyPublisher | ofByteArray | Returns a request body publisher whose body is the given byte array. | 
| static HttpRequest.BodyPublisher | ofByteArray | Returns a request body publisher whose body is the content of the given byte array of  lengthbytes starting from the specifiedoffset. | 
| static HttpRequest.BodyPublisher | ofByteArrays | A request body publisher that takes data from an  Iterableof byte arrays. | 
| static HttpRequest.BodyPublisher | ofFile | A request body publisher that takes data from the contents of a File. | 
| static HttpRequest.BodyPublisher | ofInputStream | A request body publisher that reads its data from an  InputStream. | 
| static HttpRequest.BodyPublisher | ofString | Returns a request body publisher whose body is the given  
 String, converted using theUTF_8character set. | 
| static HttpRequest.BodyPublisher | ofString | Returns a request body publisher whose body is the given  
 String, converted using the given character set. | 
public static HttpRequest.BodyPublisher fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher)
Flow.Publisher. The returned request body publisher has an unknown content length.
 BodyPublisher and Flow.Publisher, where the amount of request body that the publisher will publish is unknown.publisher - the publisher responsible for publishing the bodypublic static HttpRequest.BodyPublisher fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher, long contentLength)
Flow.Publisher. The returned request body publisher has the given content length.  The given contentLength is a positive number, that represents the exact amount of bytes the publisher must publish.
 BodyPublisher and Flow.Publisher, where the amount of request body that the publisher will publish is known.publisher - the publisher responsible for publishing the bodycontentLength - a positive number representing the exact amount of bytes the publisher will publishIllegalArgumentException - if the content length is non-positivepublic static HttpRequest.BodyPublisher ofString(String body)
 String, converted using the UTF_8 character set.body - the String containing the bodypublic static HttpRequest.BodyPublisher ofString(String s, Charset charset)
 String, converted using the given character set.s - the String containing the bodycharset - the character set to convert the string to bytespublic static HttpRequest.BodyPublisher ofInputStream(Supplier<? extends InputStream> streamSupplier)
InputStream. A Supplier of InputStream is used in case the request needs to be repeated, as the content is not buffered. The Supplier may return null on subsequent attempts, in which case the request fails.streamSupplier - a Supplier of open InputStreamspublic static HttpRequest.BodyPublisher ofByteArray(byte[] buf)
buf - the byte array containing the bodypublic static HttpRequest.BodyPublisher ofByteArray(byte[] buf, int offset, int length)
length bytes starting from the specified offset.buf - the byte array containing the bodyoffset - the offset of the first bytelength - the number of bytes to useIndexOutOfBoundsException - if the sub-range is defined to be out of boundspublic static HttpRequest.BodyPublisher ofFile(Path path) throws FileNotFoundException
 Security manager permission checks are performed in this factory method, when the BodyPublisher is created. Care must be taken that the BodyPublisher is not shared with untrusted code.
path - the path to the file containing the bodyFileNotFoundException - if the path is not foundSecurityException - if opening the file for reading is denied: in the case of the system-default file system provider, and a security manager is installed, checkRead is invoked to check read access to the given filepublic static HttpRequest.BodyPublisher ofByteArrays(Iterable<byte[]> iter)
Iterable of byte arrays. An Iterable is provided which supplies Iterator instances. Each attempt to send the request results in one invocation of the Iterable.iter - an Iterable of byte arrayspublic static HttpRequest.BodyPublisher noBody()
public static HttpRequest.BodyPublisher concat(HttpRequest.BodyPublisher... publishers)
BodyPublisher that publishes a request body consisting of the concatenation of the request bodies published by a sequence of publishers. If the sequence is empty an empty publisher is returned. Otherwise, if the sequence contains a single element, that publisher is returned. Otherwise a concatenation publisher is returned.
The request body published by a concatenation publisher is logically equivalent to the request body that would have been published by concatenating all the bytes of each publisher in sequence.
Each publisher is lazily subscribed to in turn, until all the body bytes are published, an error occurs, or the concatenation publisher's subscription is cancelled. The concatenation publisher may be subscribed to more than once, which in turn may result in the publishers in the sequence being subscribed to more than once.
 The concatenation publisher has a known content length only if all publishers in the sequence have a known content length. The contentLength reported by the concatenation publisher is computed as follows: 
Long.MAX_VALUE, the resulting content length is unknown.0 and Long.MAX_VALUE, inclusive.publishers - a sequence of publishers.
    © 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
    https://docs.oracle.com/en/java/javase/21/docs/api/java.net.http/java/net/http/HttpRequest.BodyPublishers.html