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
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.
- The request is built with
HttpRequest.newBuilder(): the URI, one.header(name, value)per header, then the method with a body publisher. - 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. -Fuploads: HttpClient has no multipart builder, so the code writes the parts with smalladdFieldandaddFilehelpers and a random boundary.-u user:passbecomes anAuthorization: Basicheader. Java’sAuthenticatoronly answers a 401 challenge, while curl sends the credentials up front.--max-timebecomes.timeout(Duration.ofSeconds(n)); the code uses 30 seconds when the command has no limit.- Like curl, HttpClient doesn’t follow redirects unless told to, so
-Lbecomes.followRedirects(HttpClient.Redirect.NORMAL).-xbecomes.proxy(ProxySelector.of(...)). - 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 -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}'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
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
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
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
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.
Save it as Main.java and run java Main.java — Java 11 and later can run a single source file without compiling it first. In a project, move the code into your own class.
Java has no built-in JSON parser. Add Jackson or Gson and read response.body() into a class or a map — for example new ObjectMapper().readTree(response.body()) with Jackson.
Yes — the request is the same. With OkHttp, build a Request with .url(), .header() and .post(RequestBody.create(json, MediaType.get("application/json"))). With Spring, RestClient takes the same URI, headers and body.
HttpClient sets a few headers itself and throws IllegalArgumentException if you try to set them: Connection, Content-Length, Expect, Host and Upgrade. The converter drops them for you.
Popular uses
Read the guide
Related tools
- Prompt CompressorCut tokens from prompts and code before you send them to an LLM.AI Dev
- Regex ExplainerPaste a regex, get plain English plus test cases.AI Dev
- AI SQL Query GeneratorEnglish to SQL for Postgres, MySQL and MSSQL.AI Dev
- JSON to Pydantic / TSInstant Pydantic models, TypeScript types or dataclasses.AI Dev