Example
curl -X PUT https://api.example.com/v1/users/42 \
-H 'Authorization: Bearer YOUR_TOKEN' \
-H 'Content-Type: application/json' \
-d '{"name": "Ada", "role": "admin"}' \
-m 10
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();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/v1/users/42"))
.header("Authorization", "Bearer YOUR_TOKEN")
.header("Content-Type", "application/json")
.timeout(Duration.ofMillis(10000))
.PUT(HttpRequest.BodyPublishers.ofString("{\"name\": \"Ada\", \"role\": \"admin\"}"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}
curl options in HttpClient
| curl | Java |
|---|---|
-X with a body | .POST(…), .PUT(…) or .method("PATCH", …) |
-H | .header(name, value) |
-d | HttpRequest.BodyPublishers.ofString(body) |
-T file | BodyPublishers.ofFile(Path.of(file)) |
-u user:pass | Authorization: Basic header with Base64 (or an Authenticator) |
-m 10 | .timeout(Duration.ofSeconds(10)) on the request |
--connect-timeout 3 | .connectTimeout(Duration.ofSeconds(3)) on the client |
-L | .followRedirects(HttpClient.Redirect.NORMAL) |
-x host:port | .proxy(ProxySelector.of(new InetSocketAddress(host, port))) |
Unlike curl and most libraries, HttpClient does not follow redirects by default (Redirect.NEVER), so the
converter only enables them for -L. Reuse one HttpClient instance across requests; it is
thread-safe and keeps a connection pool. To map the JSON response to classes, generate them with
JSON to Java.
Frequently asked questions
Which Java version does the code need?
Java 11 or later, which added java.net.http.HttpClient to the standard library. No dependencies are needed. The generated code is a complete Main class: save it as Main.java and run java Main.java (Java 11+ can run a single source file directly).
Why are some headers left out?
HttpClient manages Connection, Content-Length, Expect, Host and Upgrade itself and throws IllegalArgumentException if you set them. The converter drops them and adds a comment. Content-Length is computed from the body publisher automatically.
How are multipart uploads (-F) converted?
HttpClient has no multipart builder, so the generated code writes the multipart body itself: a random boundary, one part per field or file, and the matching Content-Type header. It sends exactly what curl sends. If you already use OkHttp or Apache HttpClient, their MultipartBody and MultipartEntityBuilder are shorter.
Why does the code force HTTP/1.1 for http:// URLs?
For plain http:// URLs, HttpClient tries to upgrade the connection to HTTP/2 by sending Upgrade: h2c and HTTP2-Settings headers. curl doesn’t, and some servers and proxies mishandle the upgrade. Over https:// the version is negotiated during the TLS handshake, so nothing is added.
How do I convert curl -k to Java?
There is no single switch. You need an SSLContext initialised with a TrustManager that accepts every certificate, passed to HttpClient.newBuilder().sslContext(…). It disables protection against man-in-the-middle attacks, so use it only in tests. For a private CA, import its certificate into a trust store instead.