// Created on savesnippets.com ยท https://savesnippets.com/T6VW4vTlBXnlbb import java.net.URI; import java.net.http.*; import java.time.Duration; class Demo { static final HttpClient client = HttpClient.newHttpClient(); HttpResponse postJson(String url, String token, String bodyJson) throws Exception { var req = HttpRequest.newBuilder() .uri(URI.create(url)) .POST(HttpRequest.BodyPublishers.ofString(bodyJson)) .header("Content-Type", "application/json") .header("Accept", "application/json") .header("Authorization", "Bearer " + token) .timeout(Duration.ofSeconds(15)) .build(); return client.send(req, HttpResponse.BodyHandlers.ofString()); } public static void main(String[] args) throws Exception { var body = """ { "name": "Alice", "email": "a@x.com" } """; var r = new Demo().postJson("https://api.example.com/users", System.getenv("API_TOKEN"), body); System.out.println(r.statusCode() + " " + r.body()); } }