cURL to Java

Paste a curl command and get Java 11 HttpClient code that compiles with nothing but the JDK.

cURL to Java runs entirely in your browser. Commands you paste — including any tokens in them — are converted on your device and never uploaded.

Open cURL to C#

About cURL to Java

Java finally got a decent HTTP client in version 11, and it removed the main reason people reached for OkHttp or Apache HttpClient just to reproduce a curl command. The builder is clean but opinionated: GET and DELETE take no body, POST and PUT take a body publisher, and anything else — PATCH included — has to go through the generic method call. Get that wrong and the code fails to compile or silently drops the payload. This converter parses the curl flags and emits code that respects those rules, adds the imports the request actually needs, and turns a -u flag into a properly encoded Authorization header.

Features

How to use the cURL to Java

  1. Paste the curl command with its headers and body
  2. Read the generated Java as it appears
  3. Copy it, or download Main.java
  4. Compile with any JDK 11 or newer — there is nothing else to install

Example

Input

curl -X PATCH https://api.example.com/users/1 -d '{"role":"admin"}'

Output

.method("PATCH", HttpRequest.BodyPublishers.ofString("{\"role\":\"admin\"}"))

The builder has no PATCH shortcut, so it goes through method() — a detail that trips up hand-written ports.

Common errors & troubleshooting

Frequently asked questions

How do I convert curl to Java?
Paste the command above. The method, URL, headers and body are parsed and rewritten as a java.net.http request, with the right builder call for the verb and the imports the code needs.
Does the generated code need any dependencies?
No. It uses java.net.http, which is part of the JDK from Java 11 onward, so a plain javac compile works with nothing on the classpath.
How do I send a PATCH request in Java?
Through the generic builder call: .method("PATCH", HttpRequest.BodyPublishers.ofString(body)). The dedicated shortcuts only exist for GET, POST, PUT and DELETE.
How do I add Basic authentication in java.net.http?
Base64-encode user:password and set it as an Authorization header with the Basic prefix. The generated code uses java.util.Base64 with an explicit UTF-8 charset, which avoids platform-dependent encoding.
Is the request sent when I convert it?
No. This tool only rewrites text — nothing is executed, and the command you paste is converted in your browser.

Related tools

All ArrayKit tools