Creating a body from a string

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

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

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