HttpRequest.BodyPublishers (Java SE 11 & JDK 11 [ad-hoc build]) (original) (raw)


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

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2018, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
DRAFT 11-internal+0-adhoc.chhegar.open