cURL to Java
Paste a curl command and get Java 11 HttpClient code that compiles with nothing but the JDK.
cURL to Java runs entirely in your browser. Commands you paste — including any tokens in them — are converted on your device and never uploaded.
Open cURL to C#
About cURL to Java
Java finally got a decent HTTP client in version 11, and it removed the main reason people reached for OkHttp or Apache HttpClient just to reproduce a curl command. The builder is clean but opinionated: GET and DELETE take no body, POST and PUT take a body publisher, and anything else — PATCH included — has to go through the generic method call. Get that wrong and the code fails to compile or silently drops the payload. This converter parses the curl flags and emits code that respects those rules, adds the imports the request actually needs, and turns a -u flag into a properly encoded Authorization header.
Features
- Emits java.net.http code that compiles with only the JDK
- Correct builder call for each verb, including PATCH through method()
- DELETE with a body falls back to the generic form automatically
- Bodies wrapped in BodyPublishers.ofString with proper escaping
- Basic auth encoded with java.util.Base64 rather than a raw header
- Imports added only for the features the request uses
- Warns about curl flags with no client-code equivalent
- Download the result as a ready-to-compile Main.java
How to use the cURL to Java
- Paste the curl command with its headers and body
- Read the generated Java as it appears
- Copy it, or download Main.java
- Compile with any JDK 11 or newer — there is nothing else to install
Example
Input
curl -X PATCH https://api.example.com/users/1 -d '{"role":"admin"}'
Output
.method("PATCH", HttpRequest.BodyPublishers.ofString("{\"role\":\"admin\"}"))
The builder has no PATCH shortcut, so it goes through method() — a detail that trips up hand-written ports.
Common errors & troubleshooting
- There is no .PATCH() method on the builder. — Correct — Java's builder only shortcuts GET, POST, PUT and DELETE. Everything else uses method(name, publisher), which the generated code does automatically.
- A DELETE with a body loses the payload. — DELETE() takes no publisher, so the body is silently dropped. The converter switches to method("DELETE", …) whenever the curl command includes data.
- The response body is empty or garbled. — Check the body handler. BodyHandlers.ofString() decodes as UTF-8 by default; a response in another charset needs the overload that takes a Charset, or ofByteArray followed by your own decoding.
Frequently asked questions
- How do I convert curl to Java?
- Paste the command above. The method, URL, headers and body are parsed and rewritten as a java.net.http request, with the right builder call for the verb and the imports the code needs.
- Does the generated code need any dependencies?
- No. It uses java.net.http, which is part of the JDK from Java 11 onward, so a plain javac compile works with nothing on the classpath.
- How do I send a PATCH request in Java?
- Through the generic builder call: .method("PATCH", HttpRequest.BodyPublishers.ofString(body)). The dedicated shortcuts only exist for GET, POST, PUT and DELETE.
- How do I add Basic authentication in java.net.http?
- Base64-encode user:password and set it as an Authorization header with the Basic prefix. The generated code uses java.util.Base64 with an explicit UTF-8 charset, which avoids platform-dependent encoding.
- Is the request sent when I convert it?
- No. This tool only rewrites text — nothing is executed, and the command you paste is converted in your browser.
Related tools
- cURL to Fetch — Convert a curl command to a JavaScript fetch() call.
- cURL to Python — Convert a curl command to Python requests code.
- cURL to C# — Convert a curl command to C# HttpClient code.
- cURL to Go — Convert a curl command to Go net/http code.
- cURL to Rust — Convert a curl command to Rust reqwest code.
- API Request Client — Send HTTP requests, build headers and params, inspect responses — your last 25 requests are saved locally.
All ArrayKit tools