cURL to Java

Convert a cURL command into a Java HttpClient request.

cURL Command
Java (HttpClient) Output
Java (HttpClient) code appears here

Related Tools

Documentation

What is curl-to-Java?

This tool converts a curl command into Java code using java.net.http.HttpClient — the HTTP client that's been built into the JDK itself since Java 11, so the output needs no OkHttp, Apache HttpClient, or any other dependency to compile and run.

How it works

The curl command is parsed into method, URL, headers, body, form fields, and auth by the same parser every cURL converter on this site shares. Headers map onto HttpRequest.Builder.header(...) calls, and the body is passed to BodyPublishers.ofString(...) as a raw string — never re-parsed and reconstructed as a Java object, so the exact payload curl would send is preserved byte for byte. -u user:pass becomes a manually built Authorization header using Base64.getEncoder().encodeToString(...), since HttpClient has no dedicated Basic Auth helper the way some higher-level clients do.

Features

  • Zero external dependencies — java.net.http.HttpClient ships with the JDK (Java 11+)
  • Body passed through as a raw string, not re-encoded
  • -u Basic Auth computed via Base64.getEncoder()
  • Synchronous request via client.send(...), printing status code and body
  • -F/--form uploads listed explicitly as a comment rather than a guessed multipart implementation

Example

Input: curl -X POST https://api.example.com/v1/users -H "Content-Type: application/json" -d '{"name":"Alice"}'

Output:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.util.Base64;

public class Main {
    public static void main(String[] args) throws Exception {
        HttpClient client = HttpClient.newHttpClient();

        HttpRequest.Builder builder = HttpRequest.newBuilder()
            .uri(URI.create("https://api.example.com/v1/users"))
            .header("Content-Type", "application/json")
            .method("POST", BodyPublishers.ofString("{\"name\":\"Alice\"}"));

        HttpRequest request = builder.build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println(response.statusCode());
        System.out.println(response.body());
    }
}

Common errors

A -F/--form command produces a request built with BodyPublishers.noBody() and a comment listing the fields, not a working multipart request — HttpClient has no multipart body builder, and hand-rolling a boundary-delimited stream is exactly the kind of "reconstructed it wrong" bug this generator avoids by refusing to fabricate. If you need real multipart support, add OkHttp and use its MultipartBody instead.

Best practices

Check response.statusCode() before trusting the body — client.send(...) only throws on I/O failures (timeouts, connection resets), never on a 4xx/5xx HTTP status. For anything beyond a quick script, consider client.sendAsync(...) instead of the synchronous call shown here, so a slow endpoint doesn't block a calling thread you actually need.

Frequently Asked Questions

Does this need a library like OkHttp or Apache HttpClient?

No — java.net.http.HttpClient has been part of the JDK itself since Java 11, so the generated code compiles and runs with zero extra dependencies on any reasonably current JDK.

How is multipart/form-data (-F) handled?

It isn't auto-generated into a working request — java.net.http.HttpClient has no built-in multipart body builder, unlike okhttp's MultipartBody. The output lists the form fields as a comment instead of fabricating a hand-rolled boundary-stream implementation that's easy to get subtly wrong; if you need multipart uploads regularly, pulling in OkHttp is the more common real-world path anyway.

How is Basic Auth handled?

Via java.util.Base64.getEncoder(), added directly as an Authorization header on the request builder — HttpClient has no built-in "basic auth" convenience method, so this is the standard way to do it with the JDK's own APIs.

What Java version does the output need?

Java 11 or later, since that's when java.net.http.HttpClient was introduced as a standard part of the JDK. Earlier versions have no equivalent in the standard library.