Convert cURL to Java (HttpClient)

Paste a curl command to get a complete Java class that uses the HttpClient built into Java — no OkHttp or Apache dependency. Save it as Main.java and run java Main.java.

AI DevRuns in your browser — nothing uploaded

Output language

Converted in your browser — tokens and passwords in the command never leave your device. Tip: in Chrome DevTools, right-click a request in the Network tab → Copy → Copy as cURL.

How curl options map to Java’s HttpClient

Java’s HttpClient has been part of the JDK since Java 11. The converted code also uses a text block for JSON, so it needs Java 17 or newer.

  1. The request is built with HttpRequest.newBuilder(): the URI, one .header(name, value) per header, then the method with a body publisher.
  2. A JSON body goes in a text block and is sent with BodyPublishers.ofString. HttpClient doesn’t set Content-Type, so the header from the command is kept.
  3. -F uploads: HttpClient has no multipart builder, so the code writes the parts with small addField and addFile helpers and a random boundary.
  4. -u user:pass becomes an Authorization: Basic header. Java’s Authenticator only answers a 401 challenge, while curl sends the credentials up front.
  5. --max-time becomes .timeout(Duration.ofSeconds(n)); the code uses 30 seconds when the command has no limit.
  6. Like curl, HttpClient doesn’t follow redirects unless told to, so -L becomes .followRedirects(HttpClient.Redirect.NORMAL). -x becomes .proxy(ProxySelector.of(...)).
  7. HttpClient refuses to set Connection, Content-Length, Expect, Host and Upgrade headers, so the converter leaves them out.

send() waits for the response; use sendAsync() to get a CompletableFuture instead. A 404 or 500 isn’t an exception, so check response.statusCode(). curl -k has no simple HttpClient option, so the code leaves a comment rather than a trust-everything SSLContext.

Example: curl to Java

This command, pasted into the converter above, gives the code below.

cURL command
curl -X PUT https://api.example.com/v1/users/42 \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"name": "Asha Rao", "role": "admin", "notify": true}'
Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

public class Main {
    public static void main(String[] args) throws Exception {
        HttpClient client = HttpClient.newHttpClient();

        String json = """
                {
                    "name": "Asha Rao",
                    "role": "admin",
                    "notify": true
                }""";

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.example.com/v1/users/42"))
                .timeout(Duration.ofSeconds(30))
                .header("Authorization", "Bearer YOUR_API_KEY")
                .header("Content-Type", "application/json")
                .PUT(HttpRequest.BodyPublishers.ofString(json))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.statusCode());
        System.out.println(response.body());
    }
}

How it works

  1. 1

    Reads the command like a shell

    Single and double quotes, $'…' strings, backslash line breaks and Windows ^ escapes are all understood — so Chrome’s “Copy as cURL (bash)” and “(cmd)” both work.

  2. 2

    Understands curl’s options

    -X, -H, -d, --data-raw, --data-urlencode, --json, -F, -u, -b, -G, -k, -L, -x, --max-time and more — with curl’s own defaults, like POST when there’s a body.

  3. 3

    Writes idiomatic code

    json= in Python, URLSearchParams and FormData in JavaScript, a multipart writer in Go, CURLFile in PHP and a text block in Java — the way each language would normally do it, using only what ships with it where possible.

  4. 4

    Tested against real curl

    Code in all seven languages was run against a test server and checked to send the same method, URL, headers and body as curl itself.

Questions, answered

Java 17 or newer. HttpClient arrived in Java 11, and the JSON text blocks need Java 15; 17 is the oldest long-term-support release with both.

Popular uses

Read the guide