Skip to content
koducathome

cURL to Java HttpURLConnection Converter

Convert curl commands to Java code using HttpURLConnection. Paste a command and the HttpURLConnection snippet appears instantly — everything runs in your browser.

How to use this tool

Steps for using the cURL to Code.

  1. Copy a curl command — from API documentation, or from your browser's DevTools (Network tab → right-click a request → Copy as cURL (bash)).
  2. Paste it into Enter cURL command. The Java HttpURLConnection code appears instantly; other Java libraries (HttpClient, jsoup, OkHttp) are in the tabs above the code.
  3. Set up: Nothing to install — it's part of java.net.
  4. Run it: Save as Main.java, compile with javac Main.java and run java Main. Replace placeholders such as YOUR_TOKEN first, and check any notes under the editors for curl options that have no HttpURLConnection equivalent.

Example

GET with headers → Java HttpURLConnection

curl command
curl 'https://api.example.com/v1/users?page=2' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer YOUR_TOKEN'
Java HttpURLConnection
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://api.example.com/v1/users?page=2");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Authorization", "Bearer YOUR_TOKEN");

        int status = conn.getResponseCode();
        System.out.println(status);
        InputStream stream = status >= 400 ? conn.getErrorStream() : conn.getInputStream();
        if (stream != null) {
            try (Scanner scanner = new Scanner(stream, StandardCharsets.UTF_8).useDelimiter("\\A")) {
                System.out.println(scanner.hasNext() ? scanner.next() : "");
            }
        }
    }
}

Common use cases

  • Older Java versions
  • Code that must avoid third-party libraries

Converting curl to Java HttpURLConnection

HttpURLConnection is the original HTTP API in the JDK. It works on every Java version, including old ones and Android.

The converter reads the curl command the way curl does — the method (-X), headers (-H), request bodies (-d, --data-raw, --json, -F), query strings, basic auth (-u) and cookies — and writes the equivalent HttpURLConnection code. Options that have no HttpURLConnection equivalent are listed under the editors rather than silently dropped.

Other libraries for Java, each with its own page: HttpClient, jsoup, OkHttp.

Limitations

  • The API is verbose; on Java 11+ prefer HttpClient.
  • The code is a starting point: add timeouts, retries and error handling for production use.
  • Secrets in the curl command are copied into the code — move them to environment variables before committing.

Frequently asked questions

Why read getErrorStream for errors?

HttpURLConnection throws if you read the normal stream of a 4xx/5xx response; the error stream holds the body.

HttpURLConnection or HttpClient?

Use HttpClient on Java 11+. Use HttpURLConnection for older Java or very minimal setups.

Is my curl command sent to a server?

No. The conversion runs entirely in your browser, so tokens and passwords in the command never leave your device. Still, replace real secrets before sharing the generated code.