1//Exemple :
2package com.zetcode;
3
4import com.fasterxml.jackson.databind.ObjectMapper;
5
6import java.io.IOException;
7import java.net.URI;
8import java.net.http.HttpClient;
9import java.net.http.HttpRequest;
10import java.net.http.HttpResponse;
11import java.util.HashMap;
12
13public class HttpClientPost {
14
15 public static void main(String[] args) throws IOException, InterruptedException {
16
17 var values = new HashMap<String, String>() {{
18 put("name", "John Doe");
19 put ("occupation", "gardener");
20 }};
21
22 var objectMapper = new ObjectMapper();
23 String requestBody = objectMapper
24 .writeValueAsString(values);
25
26 HttpClient client = HttpClient.newHttpClient();
27 HttpRequest request = HttpRequest.newBuilder()
28 .uri(URI.create("https://httpbin.org/post"))
29 .POST(HttpRequest.BodyPublishers.ofString(requestBody))
30 .build();
31
32 HttpResponse<String> response = client.send(request,
33 HttpResponse.BodyHandlers.ofString());
34
35 System.out.println(response.body());
36 }
37}
38