Convert curl to Java
Convert curl commands to Java code using the modern HttpClient API. curl2code runs entirely in your browser via WebAssembly — no data leaves your device. For Kotlin development, try our curl to Kotlin converter. For C# (.NET), see curl to C#. Below are ready-to-use Java examples.
How to copy curl from your browser
- 1
Open DevTools
Press F12 or Ctrl+Shift+I to open your browser developer tools.
- 2
Go to the Network tab
Click the Network tab and perform the action that triggers the HTTP request.
- 3
Copy as cURL
Right-click the request → Copy → Copy as cURL. Then paste it above.
Frequently Asked Questions
What is Java HttpClient?
Java's HttpClient (introduced in Java 11) is a modern, built-in HTTP client that replaces the legacy HttpURLConnection. It supports HTTP/2, async requests, WebSocket, and a fluent builder API. No external dependencies needed. curl2code also supports OkHttp, Jsoup, and HttpURLConnection as alternatives.
HttpClient vs OkHttp vs HttpURLConnection — which to use?
HttpClient (Java 11+) is the modern standard — use it for new projects. OkHttp is mature, widely used in Android, with interceptors and connection pooling. HttpURLConnection works on all Java versions but is verbose. For Android/Kotlin, see our curl to Kotlin converter. For a similar modern approach, try curl to C#.
How to handle authentication in Java?
Set the Authorization header: request.header("Authorization", "Bearer " + token). For Basic auth, encode with Base64.getEncoder().encodeToString((user + ":" + pass).getBytes()). HttpClient also supports Authenticator for automatic credential handling.
How to send multipart form data in Java?
Java 11+ HttpClient requires manual multipart body construction with boundary strings. Use HttpRequest.BodyPublishers.ofString() with manually formatted parts. OkHttp simplifies this with MultipartBody.Builder. curl2code generates the appropriate multipart code for your chosen Java library.
How to handle HTTP errors in Java?
With HttpClient, check response.statusCode() after each request — it doesn't throw on HTTP errors. Wrap calls in try/catch for IOException (network) and InterruptedException (async). OkHttp throws IOException on failures and provides response.isSuccessful() for status checks.
How to make async HTTP requests in Java?
HttpClient provides sendAsync() returning CompletableFuture: client.sendAsync(request, BodyHandlers.ofString()).thenApply(HttpResponse::body). Chain with thenAccept(), thenCompose(), or use CompletableFuture.allOf() for parallel requests. OkHttp uses enqueue() with a Callback interface.
How to set timeouts in Java HttpClient?
Set on the client: HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(). Per-request: request.timeout(Duration.ofSeconds(30)). OkHttp uses OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).
How to configure a proxy in Java?
With HttpClient: HttpClient.newBuilder().proxy(ProxySelector.of(new InetSocketAddress("proxy", 8080))).build(). OkHttp uses Proxy class: new OkHttpClient.Builder().proxy(new Proxy(Proxy.Type.HTTP, address)). Java also respects -Dhttp.proxyHost JVM flags. curl2code converts -x flags automatically.
How to send a POST request with JSON body in Java?
Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d "{"key":"value"}" URL to Java using curl2code. The generated code uses HttpRequest.newBuilder().POST(BodyPublishers.ofString(jsonString)).build(). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the HttpClient code.
How to add Bearer token authorization in Java?
Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates HttpClient code with the auth header: .header("Authorization", "Bearer YOUR_TOKEN"). curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.
How to set Content-Type header in Java?
curl2code converts curl -H "Content-Type: application/json" URL to HttpClient code with the proper header: .header("Content-Type", "application/json"). For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct HttpClient API.