import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpRequest.Builder; import java.net.http.HttpResponse.BodyHandlers; public class Client { public static void get() throws IOException, InterruptedException { String url = "https://jsonplaceholder.typicode.com/users"; HttpClient client = HttpClient.newHttpClient(); Builder request = HttpRequest.newBuilder(); request.uri(URI.create(url)); request.GET(); HttpResponse res = client.send(request.build(), BodyHandlers.ofString()); System.out.println(res.body()); } public static void post() throws IOException, InterruptedException { String url = "https://jsonplaceholder.typicode.com/users"; HttpClient client = HttpClient.newHttpClient(); Builder request = HttpRequest.newBuilder(); request.uri(URI.create(url)); request.header("Content-Type", "application/json"); String body = "{ \"name\": \"John\", \"age\": 30 }"; request.POST(HttpRequest.BodyPublishers.ofString(body)); HttpResponse res = client.send(request.build(), BodyHandlers.ofString()); System.out.println(res.body()); } public static void put() throws IOException, InterruptedException { String url = "https://jsonplaceholder.typicode.com/users/1"; HttpClient client = HttpClient.newHttpClient(); Builder request = HttpRequest.newBuilder(); request.uri(URI.create(url)); request.header("Content-Type", "application/json"); String body = "{ \"name\": \"John\", \"age\": 30 }"; request.PUT(HttpRequest.BodyPublishers.ofString(body)); HttpResponse res = client.send(request.build(), BodyHandlers.ofString()); System.out.println(res.body()); } public static void delete() throws IOException, InterruptedException { String url = "https://jsonplaceholder.typicode.com/users/1"; HttpClient client = HttpClient.newHttpClient(); Builder request = HttpRequest.newBuilder(); request.uri(URI.create(url)); request.DELETE(); HttpResponse res = client.send(request.build(), BodyHandlers.ofString()); System.out.println(res.body()); } }