Setting a request body can be accomplished using HttpRequest.Builder.POST() and HttpRequest.Builder.PUT() or by using method() (for example, method("PATCH", HttpRequest.BodyPublisher)). POST() and PUT() take an argument of the HttpRequest.BodyPublisher type. The API comes with several implementations of this interface (BodyPublisher) in the HttpRequest.BodyPublishers class, as follows:
- BodyPublishers.ofString()
- BodyPublishers.ofFile()
- BodyPublishers.ofByteArray()
- BodyPublishers.ofInputStream()
We'll take a look at these implementations in the following sections.