Creating a body from a byte array

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).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.156.251