cURL to Java
Convert a curl command to Java HttpClient
About cURL to Java
Produces Java 11+ code for the built-in java.net.http.HttpClient: an HttpRequest.newBuilder() chain with one .header() per -H and a .GET(), .POST(), .PUT(), .DELETE() or .method() call. Bodies are sent with BodyPublishers.ofString and the response is read synchronously as a string. No Maven or Gradle dependency is needed, and conversion runs locally in your browser.
Category: API.
Frequently asked questions
Which Java version and dependencies does the code need?
▾
Java 11 or later, since it uses the built-in java.net.http.HttpClient and var declarations; no Maven or Gradle dependency is required. The output is a list of statements, so paste it into a method that declares throws IOException, InterruptedException, because client.send throws both.
Is the Java HttpClient call synchronous?
▾
Yes. client.send blocks until the response arrives and reads it as a string with HttpResponse.BodyHandlers.ofString(). For non-blocking code, switch to client.sendAsync, which returns a CompletableFuture.
Why doesn't the Java request follow redirects with -L?
▾
-L is not translated, and HttpClient.newHttpClient() uses the NEVER redirect policy. Build the client with HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build() to follow them; -k is dropped as well and would require a custom SSLContext.
How are -F fields and -u credentials encoded in Java?
▾
-F fields are percent-encoded into a single name=value&name=value string sent with BodyPublishers.ofString, not as multipart, and no Content-Type header is added for them. -u user:pass becomes an Authorization: Basic header set with .header(), with the Base64 value precomputed.