cURL → Java
Java HTTP Client Generator

cURL to Java Converter (Java 11+ HttpClient, OkHttp & Spring WebClient)

Convert cURL commands into production-grade Java HTTP requests with native java.net.http.HttpClient, OkHttp3, or Spring WebClient.

100% Client-Side Privacy
Zero Server Transmission
Presets:
cURL Command Input
// Java 11+ Native java.net.http.HttpClient
// Built into JDK 11, 17, 21+ standard library — zero external dependencies required!

package com.example.client;

import java.io.IOException;
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 HttpClientExample {

    public static void main(String[] args) {
    try {
        // 1. Configure and build HttpClient
        HttpClient client = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_2)
            .connectTimeout(Duration.ofSeconds(30))
            .build();

        // 2. Prepare request payload
        String requestBody = "{\"title\": \"Bug in auth session\", \"body\": \"Need to verify token expiration handling\", \"labels\": [\"bug\", \"priority\"]}";

        // 3. Build HttpRequest
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
            .uri(URI.create("https://api.github.com/repos/octocat/Hello-World/issues"))
            .timeout(Duration.ofSeconds(30))
            .header("Accept", "application/vnd.github+json")
            .header("Authorization", "Bearer gho_16C7e42F292c6912E7710c838347Ae178B4a")
            .header("X-GitHub-Api-Version", "2022-11-28")
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(requestBody));
        HttpRequest request = requestBuilder.build();

        // Synchronous execution
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println("Status Code: " + response.statusCode());
        System.out.println("Response Body:\n" + response.body());
    } catch (Exception e) {
        System.err.println("HTTP Client Error: " + e.getMessage());
        e.printStackTrace();
    }
    }
}
HTTP Method & Protocol
POSTHTTPS
Target Host & Route
https://api.github.com
0 query params
Headers & Payload
4 headers • BEARER Auth
JSON Payload (115 B)

100% Client-Side Transpilation

cURL syntax parsing, argument tokenization, and Java source synthesis run entirely inside your browser memory without server network round-trips.

Modern Java 11+ & Enterprise Ecosystem

Transpiles terminal commands into Native java.net.http.HttpClient, OkHttp 3/4, Spring WebClient (Reactive), Spring RestTemplate, and Apache HttpClient 5.

Complete Flag & Auth Parity

Accurately parses headers (-H), Bearer/Basic credentials (-u), timeouts (-m), SSL bypass flags (-k), redirects (-L), and complex JSON payloads into idiomatic Java.

Technical Reference & Architecture Guide

cURL to Java Converter (Java 11+ HttpClient, OkHttp & Spring WebClient) Developer Guide — Reference Guide

Convert cURL commands into production-grade Java HTTP requests with native java.net.http.HttpClient, OkHttp3, or Spring WebClient.

Key Parameters & Controls

clientTargetstring

Target Java HTTP library framework: java.net.http.HttpClient (Java 11+), Square OkHttp3, or Spring 5/6 WebClient.

Default: HttpClient (Java 11+)
timeoutSecondsinteger

Connection and request duration timeout in seconds configured on the client builder.

Default: 10
asyncModeboolean

Generate asynchronous non-blocking calls using CompletableFuture or Project Reactor Mono/Flux.

Default: false
cleanDevToolsboolean

Strip browser DevTools pseudo-headers (:authority, :method, :path, :scheme) to prevent Java IllegalArgumentException.

Default: true

Pro Tips & Best Practices

Reuse HttpClient as a Singleton Instance

Always create a single static instance of HttpClient or OkHttpClient per application. Instantiating a new client per request spawns unnecessary thread pools, connection managers, and SSL sessions, leading to socket exhaustion and high memory footprint.

Explicitly Specify UTF-8 in BodyPublishers

When transmitting JSON or textual payloads with Java 11+ HttpClient, always pass StandardCharsets.UTF_8: HttpRequest.BodyPublishers.ofString(jsonString, StandardCharsets.UTF_8). Omitting the charset defaults to the system file encoding, which risks data corruption on non-ASCII characters.

Leverage sendAsync() with CompletableFuture for High Throughput

For I/O-bound microservices, use client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) instead of blocking send(). Chain reactive stages with thenApply() or thenCompose() to free worker threads for concurrent workloads.

Common Gotchas & Troubleshooting

java.lang.IllegalArgumentException: restricted header name or invalid colon character

Java 11+ HttpClient strictly restricts pseudo-headers (e.g. :authority) and system headers (Connection, Content-Length, Host). Enable the Clean DevTools Headers setting to automatically strip restricted headers before builder execution.

Request hangs indefinitely with no timeout exception

Java 11+ HttpClient instances have no default request timeout. Set Duration.ofSeconds(...) explicitly on both HttpClient.newBuilder().connectTimeout(...) and HttpRequest.newBuilder().timeout(...).

HTTP 415 Unsupported Media Type returned from target server

Verify that your request builder explicitly sets header("Content-Type", "application/json; charset=utf-8") when passing a JSON string body.

Frequently Asked Questions

When should I choose Java 11+ HttpClient over OkHttp or Spring WebClient?▼

Use Java 11+ java.net.http.HttpClient for modern vanilla Java microservices without third-party dependencies. Choose OkHttp3 for Android development or when advanced interceptors and connection pool tuning are required. Use Spring WebClient in Spring Boot 2/3 reactive applications where Project Reactor (Mono/Flux) is standard.

How does cURL Basic Authentication (-u user:password) translate to Java?▼

In Java 11+ HttpClient, credentials can be set via an Authenticator on the client or manually with header("Authorization", "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes(StandardCharsets.UTF_8))). In OkHttp, Credentials.basic("user", "password") is used.

How is multipart/form-data file upload converted in Java?▼

OkHttp handles multipart natively via MultipartBody.Builder. For Java 11+ HttpClient, an RFC-compliant multipart publisher helper is generated using boundary delimiters and HttpRequest.BodyPublishers.ofFile(path).

Does this tool send my cURL command or API credentials to an external server?▼

No. 100% of the token parsing, header normalization, and Java code generation executes entirely within your browser client. No code, API keys, or headers are ever uploaded or logged.

Related Tools & Utilities

All Tools →