cURL to Java Converter

Paste a cURL command and get equivalent Java using the modern java.net.http client introduced in Java 11 — no Apache HttpClient or OkHttp dependency required. The parser reads the method, repeated headers, request body, -u basic auth and cookies, then emits an HttpClient.newHttpClient() plus an HttpRequest.newBuilder() chain and a client.send(...) call returning a String body. Everything runs in your browser.

How to use the cURL to Java Converter

Paste a cURL command into the cURL command field — a browser Copy as cURL string with backslash line continuations is fine. Press Convert to Java (it also converts live as you type) and the Java code appears below. Click Example to load a JSON POST with a bearer token so you can see how the builder is assembled, then Copy it. The output includes the java.net.http imports and a main method declared throws Exception for brevity. It builds the request via HttpRequest.newBuilder().uri(URI.create(url)), sets the method with .method(method, BodyPublishers.ofString(body)) (or BodyPublishers.noBody() when there is no payload), adds each header with .header(name, value), and for -u credentials emits an Authorization: Basic header built with Base64.getEncoder(). It sends with client.send(request, BodyHandlers.ofString()) and prints response.statusCode() and response.body().

Converting cURL to Java with java.net.http

Before Java 11 the standard options for HTTP were the clunky HttpURLConnection or a third-party library such as Apache HttpClient or OkHttp. Java 11 added java.net.http — a modern, fluent, immutable client built into the JDK. The three core types are HttpClient (the reusable, thread-safe sender), HttpRequest (built through a fluent newBuilder()), and HttpResponse<T> (typed by a BodyHandler). This converter generates that idiom so the result needs no dependencies and no build-tool changes on a Java 11+ project.

The detail that catches people out is the request body. The builder does not take a string — it takes a BodyPublisher, so a payload is wrapped in HttpRequest.BodyPublishers.ofString(...), and a body-less request uses BodyPublishers.noBody(). The method and its publisher are set together via .method(name, publisher), which also handles custom verbs like PATCH that the convenience .GET()/.POST() shortcuts do not expose. On the response side a BodyHandler decides the type; BodyHandlers.ofString() gives you a String. There is no built-in basic-auth setter on the builder, so -u user:pass is encoded with Base64.getEncoder().encodeToString(...) and added as an explicit Authorization: Basic header.

This is the focused Java path. For the same request in another ecosystem, the cURL to Python converter emits requests or httpx, and the multi-language cURL converter covers several targets at once.

Common use cases

  • No-dependency clients — call an API from a Java 11+ service using only the JDK, skipping OkHttp or Apache HttpClient.
  • DevTools to Java — turn a browser “Copy as cURL” into a working HttpClient request.
  • Spring Boot glue — get a baseline before wrapping the call in RestClient or WebClient.
  • Documentation — show an endpoint as modern Java beside the shell example.
  • Learning java.net.http — see how BodyPublishers and BodyHandlers connect a request to a typed response.
  • Porting scripts — move a curl-based job into a compiled JVM program.

Frequently asked questions

Which Java version is required?

Java 11 or newer. The java.net.http package was added in Java 11 and is part of the standard library, so no external HTTP dependency is needed.

Why is the body wrapped in BodyPublishers.ofString?

Because HttpRequest.Builder.method() takes a BodyPublisher, not a raw string. A payload uses BodyPublishers.ofString(...); a request with no body uses BodyPublishers.noBody().

How is -u basic auth handled?

There is no basic-auth setter on the builder, so the credentials are Base64-encoded with Base64.getEncoder() and added as an explicit Authorization: Basic header.

Why does it use .method() instead of .POST()?

Using .method(name, publisher) handles any verb uniformly, including PATCH and custom methods that the convenience shortcuts do not expose.

Need a different language?