Convert curl to Java HttpClient code
This curl to Java converter produces a self-contained Main class built on java.net.http.HttpClient, the HTTP client that ships with the JDK since Java 11. You do not need OkHttp, Apache HttpClient or any Maven dependency to run the result. Because it is a single file, you can launch it directly with java Main.java.
Example
Given a JSON POST with a bearer token and --max-time 30, the generated body of main reads:
HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(30)).build();HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://api.example.com/v1/orders?notify=true")).header("Content-Type", "application/json").header("Authorization", "Bearer YOUR_API_TOKEN").timeout(Duration.ofSeconds(30)).POST(HttpRequest.BodyPublishers.ofString("{\"customer_id\":42,\"gift\":false}")).build();HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());The JSON payload is embedded as an escaped Java string. If you prefer a text block, Java 15+ lets you rewrite it with triple quotes without the backslashes.
The response is read with BodyHandlers.ofString() and printed along with the status code. For large downloads, switch to BodyHandlers.ofFile(Path.of("out.bin")) so the body is streamed to disk instead of held in memory.
Supported curl options
| curl | Java |
|---|---|
-X METHOD | .GET(), .POST(), .PUT(), .DELETE() or .method("PATCH", ...) |
-H, -A, -e, -b | .header(name, value) |
-d, --data-raw, --json | HttpRequest.BodyPublishers.ofString(...) |
-u user:pass | An Authorization: Basic header built with java.util.Base64 |
-L | .followRedirects(HttpClient.Redirect.NORMAL) |
-m / --max-time | .timeout() on the request plus .connectTimeout() on the client (which uses --connect-timeout when you also pass it) |
Redirect handling matches curl out of the box: HttpClient defaults to Redirect.NEVER, so redirects are only followed when your command includes -L.
Limitations
The JDK client has no built-in multipart encoder, so a command with -F gets a comment suggesting OkHttp or Apache HttpClient instead of a half-working body. Likewise, -k cannot be expressed in one line because disabling certificate checks requires a custom SSLContext; the converter leaves a note rather than generating unsafe boilerplate. Proxy settings and client certificates are not converted.
Java's client also restricts a few headers such as Host, Connection and Content-Length, and throws an exception if you set them. Delete those lines if your copied command included them.
Making the generated class production-ready
The converter reproduces your curl command faithfully, which is ideal for a quick test. Before the code moves into an application, a few changes are worth making:
String token = System.getenv("API_TOKEN");
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() >= 400) {
throw new IllegalStateException("HTTP " + response.statusCode() + ": " + response.body());
}
- Read secrets from the environment instead of keeping the token from the curl command in source code.
- Check the status code.
send()only throws for network problems; a 404 or 500 still returns a normal response object. - Reuse one client.
HttpClientis immutable and thread-safe and keeps a connection pool, so create it once rather than per request.
Privacy
The conversion happens in your browser, and API tokens in the command are never uploaded. For other stacks, try curl to C# or curl to Python. To create Java classes for the response, use JSON to Java.