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();
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)
}
}
}
Build manually
To build the app as a JAR file:
gradlew build
Then we can run it as usual JAR file with JRE.
Build as native executable:
./gradlew nativeCompile
Run
- As Java
java -jar build/libs/http-client-java-cli-19.jar --url http://example.com --method GET
- As CLI app
jcurl --url http://example.com --method GET
So the signature is:
jcurl [arguments]
Arguments
request URI:
--url
Method (GET / POST / PUT / DELETE / HEAD)
--method
Authorization:
--auth
or
-A
as 'USER:PASSWORD'. e.g.
--auth admin:admin
Headers:
--headers
request Headers in format of:
key:value, key2:value2
query parameters
--query
for the request. will be passed as
?query=
Examples
GET
jcurl --url http://example.com --method GET
POST
jcurl --url http://example.com --method POST --body '{"name": "Vasyl"}' --headers 'Content-Type: application/json'
Top comments (0)