September 2019
Intermediate to advanced
816 pages
18h 47m
English
Creating a body from a byte array can be accomplished using BodyPublishers.ofByteArray(), as shown in the following snippet of code:
HttpRequest requestBodyOfByteArray = HttpRequest.newBuilder() .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofByteArray( Files.readAllBytes(Path.of("user.json")))) .uri(URI.create("https://reqres.in/api/users")) .build();
We can also send only a part of the byte array using ofByteArray(byte[] buf, int offset, int length). Moreover, we can provide data from Iterable of byte arrays using ofByteArrays(Iterable<byte[]> iter).