Free tool

curl to Java

Paste a curl command and get the same request in Java, written with java.net.http.HttpClient, part of the JDK since Java 11. No account; the command is only read.

An example: a POST with JSON and a token

curl -X POST 'https://api.example.com/items?page=1' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -d '{"name":"pen","price":2.5}'

becomes, in Java:

Java

import java.net.URI;
import java.net.http.*;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder(URI.create("https://api.example.com/items?page=1"))
            .header("Content-Type", "application/json")
            .header("Authorization", "Bearer YOUR_TOKEN")
            .method("POST", HttpRequest.BodyPublishers.ofString("{\"name\":\"pen\",\"price\":2.5}"))
            .build();
        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.statusCode());
        System.out.println(response.body());
    }
}

About the Java code

The code uses java.net.http, so it needs no library: an HttpRequest built with one .header per header, sent by an HttpClient.

The body is sent with HttpRequest.BodyPublishers.ofString; a request without one uses noBody(). Add .timeout(Duration.ofSeconds(30)) to the request in a real program.

response.statusCode() is the site's status and response.body() the page as a string.

What the converter understands

The address, -X, -H, -d and its variants, --json, -u, -A, -b, -e, -G and -I, quotes and lines continued with a backslash. Options that do not change the request, such as -L, -s or --compressed, are ignored.

Other languages

curl to Python, curl to JavaScript, curl to PHP, curl to Go, or all eight languages at once.

Need pages for a program, not code? gimmepage fetches any public page in one request: 1 credit a page, failed requests free.