DEV Community

Andrii Maliuta
Andrii Maliuta

Posted on

1

Java-based console app as HTTP client

So here is "Ye another one http client" 🐱 that I created for personal use as practice and just to have own tool to test HTTP requests not to download CURL or HTTpie 🐱.

Source code: https://github.com/ahndmal/http-client-java-cli/

JCurl (very hard to create name 🐱)

So this is the console app in Java to receive some arguments and perform HTTP / Socket requests to the passed web host as an URI. Here Java 11 HTTP client is used as a core. To perform more requests without blocking, async reactive processing of requests is used

// client
 HttpClient client = HttpClient.newBuilder()
      .version(HttpClient.Version.HTTP_2)
      .build();
Enter fullscreen mode Exit fullscreen mode

It is a Native and JDK based HTTP client. To build the app as a native executible, we use GraalVM (https://www.graalvm.org/).

Gradle GraalVM plugin

As a build tool we use Gradle and Gradle GraalVM plugin (https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html) that is flexible and fast to build (relatively 🐱) due to cahing.


graalvmNative {
    binaries {
        named("main") {
            javaLauncher.set(javaToolchains.launcherFor {
                languageVersion.set(JavaLanguageVersion.of(19))
            })
            mainClass.set("com.andmal.Main")
            verbose.set(true)
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Build manually

To build the app as a JAR file:

gradlew build
Enter fullscreen mode Exit fullscreen mode

Then we can run it as usual JAR file with JRE.

Build as native executable:

./gradlew nativeCompile
Enter fullscreen mode Exit fullscreen mode

Run

  1. As Java
  java -jar build/libs/http-client-java-cli-19.jar --url http://example.com --method GET
Enter fullscreen mode Exit fullscreen mode
  1. As CLI app
    jcurl --url http://example.com --method GET
Enter fullscreen mode Exit fullscreen mode

So the signature is:

jcurl [arguments]

Arguments

request URI:

  --url 
Enter fullscreen mode Exit fullscreen mode

Method (GET / POST / PUT / DELETE / HEAD)

 --method 
Enter fullscreen mode Exit fullscreen mode

Authorization:

 --auth 
Enter fullscreen mode Exit fullscreen mode


or

 -A 
Enter fullscreen mode Exit fullscreen mode

as 'USER:PASSWORD'. e.g.

 --auth admin:admin 
Enter fullscreen mode Exit fullscreen mode

Headers:

 --headers 
Enter fullscreen mode Exit fullscreen mode

request Headers in format of:

 key:value, key2:value2 
Enter fullscreen mode Exit fullscreen mode

query parameters

 --query 
Enter fullscreen mode Exit fullscreen mode

for the request. will be passed as

 ?query= 
Enter fullscreen mode Exit fullscreen mode

Examples

GET

    jcurl --url http://example.com --method GET
Enter fullscreen mode Exit fullscreen mode

POST

    jcurl --url http://example.com --method POST --body '{"name": "Vasyl"}' --headers 'Content-Type: application/json'
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay