curl → java · okhttp · headers · body · auth · live

Convert cURL to Java

Translate a cURL command into an OkHttp request. Builder pattern, fluent headers, RequestBody.create — idiomatic Java that pastes into Android, Spring or plain JVM code.

cURL → Java (OkHttp)
Input
161 B
4 lines
Output
453 B
10 lines
Status
ready
live
Ratio
2.81×
out/in
input · curl command
output · java (okhttp)
OkHttpClient client = new OkHttpClient();

Request.Builder builder = new Request.Builder()
    .url("https://api.example.com/users")
    .addHeader("Content-Type", "application/json")
    .addHeader("Authorization", "Bearer abc123")
    .post(RequestBody.create(MediaType.parse("application/json"), "{\"name\":\"Alice\",\"role\":\"admin\"}"));

Response response = client.newCall(builder.build()).execute();
System.out.println(response.body().string());
Quick Start

How to port cURL to Java (OkHttp)

OkHttp's builder pattern reads close to the cURL command itself. The generator wires up the URL, method, headers and (for non-GET) a RequestBody. Auth tokens come through as Authorization headers.

  1. 01

    Paste cURL

    From DevTools, Postman, an SDK doc — anything that emits a cURL command.

    Parsing is strict-but-tolerant — comments and trailing commas are accepted on input where the source format historically permits them; output stays canonical.

    inputstricttolerantliveparse
    in · sourcecurl · live
    curl -X POST https://api.example.com
    loaded · parsedtarget · ready
  2. 02

    Parser pulls fields

    URL, method, headers, body and basic auth are extracted into a structured shape.

    Conversion preserves type information where the target supports it (numbers stay numeric, booleans stay boolean) so the destination parser never has to re-coerce.

    parsetype-preservinglosslesslivememoised
    url · optionsheaders · live
    { url, method, headers, data }
    options applied · liveround-trip
  3. 03

    Emit Java (OkHttp)

    The generator turns the parsed command into idiomatic code for the target stack.

    Re-encoding is idempotent: running output back through the same converter produces the same bytes, which keeps round-trips git-friendly.

    genidempotentround-tripdiff-cleandeterministic
    out · convertedJava (OkHttp) · ready
    Request req = new Request.Builder()
      .url(url)
      .post(body)
      .build();
    converted · livediff-clean
  4. 04

    Copy or download

    Drop into your project and tweak as needed.

    Copy writes via navigator.clipboard.writeText; downloads use the target’s native MIME so editors apply the right syntax highlighting on open.

    exportclipboardtarget MIMEblobclient-side
    ✓ · copiedready · done
    ✓ Copied
    shipped · ready to shipsuccess
Pro tips

Converting between formats, safely

Pick the strictest source format

Lossy formats hide bugs. Choose JSON or YAML as your source-of-truth, convert downstream as needed.

Round-trip in CI

If your converter is idempotent, your CI can verify A → B → A equals the original. Catches subtle data loss.

Numbers vs strings differ

YAML/TOML have a number type; CSV doesn’t. Quote IDs in CSV to keep leading-zero values intact.

Watch character encoding

Source format may be Latin-1 / Windows-1252; target should be UTF-8. Convert encoding before structure.

Preserve comments where you can

YAML and TOML support comments. Don’t silently drop them — they often encode intent.

Schema-aware beats blind

Use a schema to guide conversion. Field types, required-ness, and defaults survive better with structure.

Use Cases

When to port cURL to Java (OkHttp)

Migration, polyglot teams, integration tests.

Spring services

Wire a downstream call into a Spring Boot client. Type information survives: numbers stay numeric, booleans stay boolean, strings stay strings. The destination parser doesn't have to re-coerce, which avoids the "'true' becomes "true"" bug class.

round-trip safe

Android apps

Mobile clients can paste the snippet into a repository class. Run output back through the inverse converter to verify idempotent round-trips in CI. A → B → A should produce byte-identical input — if it doesn't, you've found data loss.

type-preserving

JVM scripts

Use as the basis for a kscript / Kotlin Notebook call. Comments and metadata survive where the target format supports them (YAML, TOML, HCL). The tool doesn't silently drop intent-encoded annotations, which keeps your config self-documenting.

schema-aware

Tests

Mock the call with okhttp-mockwebserver afterwards. Pair with a schema (JSON Schema, OpenAPI, Zod, Pydantic) to catch shape drift on either side. Catch breaking changes at conversion time rather than at runtime in production.

lossless

Migrations

Move legacy HttpURLConnection code to OkHttp. Output respects target-format conventions: quoting in CSV, indent in YAML, brackets in JSON. Existing parsers don't need patches, and the result drops into legacy pipelines unchanged.

diff-clean

Polyglot hand-off

Hand a curl off to the JVM team in their idiom. Encoding-aware: handles UTF-8 vs UTF-16 vs Latin-1 / Windows-1252 transparently. The character-set surprise that breaks "works on my machine" doesn't apply here.

spec-grade
FAQ

Frequently asked questions

OkHttp's API stays consistent across Android and the JVM. java.net.http.HttpClient (JDK 11+) is also fine — adapt the snippet manually.
Yes — Authorization headers (Bearer, Basic) carry through. For -u user:pass, the generator emits Credentials.basic().
JSON bodies create a RequestBody with the application/json media type. Form bodies use FormBody.Builder().
Basic --data only. Use MultipartBody.Builder() manually for -F flags.
No. Parsing and code generation run locally in your browser.
Suggest a tool

Something missing?

Tell us what you need. We ship the top requests every two weeks.

Sent straight to the maintainers · no account needed
47 in queue·8 shipped this month
Request queued. Thanks!