September 2019
Intermediate to advanced
816 pages
18h 47m
English
The following solution creates a new user (the response status code should be 201):
Data data = new Data();data.setId(10);data.setFirstName("John");data.setLastName("Year");data.setAvatar("https://johnyear.com/jy.png");User newUser = new User();newUser.setData(data);HttpRequest requestPost = HttpRequest.newBuilder() .header("Content-Type", "application/json") .uri(URI.create("https://reqres.in/api/users")) .POST(HttpRequest.BodyPublishers.ofString(jsonb.toJson(user))) .build();HttpResponse<Void> responsePost = client.send( requestPost, HttpResponse.BodyHandlers.discarding());int sc = responsePost.statusCode(); // 201
Note that we ignore any response body via HttpResponse.BodyHandlers.discarding().