FormatterHQ

cURL to Java Converter

Convert a curl command into a Java class that uses the built-in java.net.http.HttpClient from Java 11 and later, with no extra dependencies.

Input SHELL
Output JAVA
Ready.

How to use the cURL to Java Converter

  1. Paste the curl command you want to translate.
  2. The Java class is generated as you type; press Convert to refresh it after changes.
  3. Check the comments at the top for options that need manual work, such as -F uploads.
  4. Save the code as Main.java and run it with java Main.java on JDK 11 or newer.

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

curlJava
-X METHOD.GET(), .POST(), .PUT(), .DELETE() or .method("PATCH", ...)
-H, -A, -e, -b.header(name, value)
-d, --data-raw, --jsonHttpRequest.BodyPublishers.ofString(...)
-u user:passAn 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. HttpClient is 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.

Frequently asked questions

Which Java version do I need for the generated code?
Java 11 or newer, because java.net.http.HttpClient was added as a standard API in JDK 11. On Java 8 you would need a library such as OkHttp or Apache HttpClient instead.
How do I send the request asynchronously?
Replace client.send(...) with client.sendAsync(request, HttpResponse.BodyHandlers.ofString()). It returns a CompletableFuture that you can chain with thenApply or wait on with join().
Why does Java throw "restricted header name"?
The JDK client manages headers like Host, Connection and Content-Length itself and refuses to let you set them. Remove those .header() calls; the request will still be sent correctly.
Can the converter handle multipart file uploads for Java?
Not with the built-in client, which has no multipart body publisher. The output adds a comment instead; OkHttp's MultipartBody or Apache HttpClient's MultipartEntityBuilder are the usual choices.

Last updated September 26, 2026

Related tools

cURL to Axios (Node.js) Converter Convert curl commands to Node.js axios requests with params, headers, JSON data, auth, form-data uploads and timeouts. cURL to C# Converter Convert curl commands to C# HttpClient code for .NET with headers, content types, basic auth, multipart and TLS options. cURL to Go Converter Convert curl commands into Go code using net/http — method, headers, JSON or form body, basic auth, timeouts and multipart uploads. cURL to PHP Guzzle Converter Convert curl commands to PHP Guzzle requests using json, form_params, multipart, query and auth options. cURL to JavaScript Fetch Convert curl commands into JavaScript fetch() code with method, headers, JSON body, form data, basic auth and timeouts. cURL to PHP Converter Convert curl commands into PHP code using the curl_* functions: method, headers, body, auth, cookies and SSL options. cURL to Python Requests Convert curl commands into Python requests code: headers, params, JSON or form data, files, cookies, auth and timeouts. cURL to Rust Converter Convert curl commands to Rust using the reqwest crate, with JSON bodies via serde_json, headers, auth and timeouts. Date & Time Calculator Calculate the difference between two dates or add/subtract days, weeks, months and years from a date. Docker Run to Docker Compose Convert one or more docker run commands into a docker-compose.yml: ports, volumes, env, restart, networks, healthchecks and more. File Type Checker Find out what a file really is from its content, not its name. Detects 150+ formats and warns when the extension does not match. HTTP Status Codes Searchable list of the HTTP status codes in use, from 100 to 511, with a plain explanation of when each response is used.