September 2019
Intermediate to advanced
816 pages
18h 47m
English
Creating a body from a string can be accomplished using BodyPublishers.ofString(), as shown in the following snippet of code:
HttpRequest requestBody = HttpRequest.newBuilder() .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString( "{\"name\": \"morpheus\",\"job\": \"leader\"}")) .uri(URI.create("https://reqres.in/api/users")) .build();
For specifying a charset call, use ofString(String s, Charset charset).